Skip to content

perf: add a plain object and array fast path to deepCompare - #12999

Open
sarankumar1325 wants to merge 3 commits into
typeorm:masterfrom
sarankumar1325:perf/deep-compare-plain-values
Open

sarankumar1325 wants to merge 3 commits into
typeorm:masterfrom
sarankumar1325:perf/deep-compare-plain-values

Conversation

@sarankumar1325

@sarankumar1325 sarankumar1325 commented Oct 23, 2026 •

Copy link
Copy Markdown

Description of change

OrmUtils.deepCompare runs on every save() for each json/jsonb and array column. For plain JSON values it pays for checks that only matter for other kinds of value: isUint8Array, the instanceof chain, isPrototypeOf, two for...in passes, and a chain push/pop for every nested property.

This adds a fast path to compare2Objects for the common case. It applies when both values are plain objects (prototype Object.prototype) or plain arrays (prototype Array.prototype) with the same prototype:

  • The fast path runs after the equals() check, so values that define equals behave as before.
  • It keeps the constructor/prototype checks and the ancestor-chain cycle check.
  • It compares own enumerable keys in one pass, using Object.keys with a key-count check and a propertyIsEnumerable lookup on the other side, so the two sides must have the same set of enumerable own keys.
  • Like before, leaves are compared with typeof + !==, and nested objects recurse through compare2Objects. So Date, Buffer/Uint8Array, Map, Set and objects with equals() inside a JSON value still use the existing logic.

Everything else (class instances, Map, Set, Date, typed arrays, null-prototype objects) still goes through the existing path unchanged.

Results (the benchmark from the issue, Node 24.22, median, old vs new compiled OrmUtils):

objects before (ms) after (ms) speedup
100 0.63 0.18 3.6x
1,000 6.26 1.40 4.5x
7,000 46.15 9.50 4.9x
20,000 139.08 27.41 5.1x

Verification

  • I added unit tests for plain object/array comparison, cyclic plain objects, and a non-enumerable property masking a missing key. Before adding them to this branch, I checked that they also pass against the current master implementation.
  • I ran a differential fuzz of 300k random pairs of old vs new deepCompare. The pairs were nested objects/arrays mixing NaN, -0, null/undefined, Date, Buffer, Uint8Array, Map, Set, sparse arrays and constructor/prototype keys. Old and new gave 0 different results.
  • The unit test suite passes, and tsc --noEmit and eslint report no new errors.

One semantic note: the old for...in loops would also pick up enumerable properties added to Object.prototype/Array.prototype (prototype pollution). The fast path only looks at own keys. It also no longer throws when a JSON object has its own hasOwnProperty key.

AI disclosure: I wrote this change with help from an AI coding assistant. I reviewed it, ran the tests and benchmarks above, and checked the results myself.

Pull-Request Checklist

@github-actions github-actions Bot added the linked-issue PR references an issue label Oct 23, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-23T19:42:49.430077Z
We couldn’t complete this review. Comment @codex review to try again.
6977f6e PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Different object fields compare as equal ✓ Resolved
Description
compareOwnKeys counts enumerable keys with Object.keys, but accepts a matching non-enumerable
key through hasOwnProperty, leaving a different enumerable key unchecked. For {a: 1} versus `{b:
2} with an additional non-enumerable a: 1 on the second object, deepCompare` returns true; if
such a value reaches the JSON save comparison, the column update is skipped.

Grey Divider

  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.

Grey Divider

Context sources
Review mode: Auto: ⚖️ Balanced: Changes core save-time comparison logic; wrong equality skips column updates.

Grey Divider

Tip of the day
💡 Did you know, you can import rules and skills only from the repositories your team follows

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Previous reviews

Review updated until commit 9a7b66e ⚖️ Balanced

Results up to commit 7243bd2


🐞 Bugs (0) 📘 Rule violations (0) 📜 Skill insights (0)


Remediation recommended
1. Different object fields compare as equal ✓ Resolved
Description
compareOwnKeys counts enumerable keys with Object.keys, but accepts a matching non-enumerable
key through hasOwnProperty, leaving a different enumerable key unchecked. For {a: 1} versus `{b:
2} with an additional non-enumerable a: 1 on the second object, deepCompare` returns true; if
such a value reaches the JSON save comparison, the column update is skipped.
Code

packages/typeorm/src/util/OrmUtils.ts[593]

+            if (!Object.prototype.hasOwnProperty.call(y, key)) return false
Evidence
Object.keys(y) counts only its enumerable b, while hasOwnProperty.call(y, "a") accepts its
non-enumerable a; the loop never checks b. The JSON/JSONB save path returns without recording a
column difference when deepCompare returns true.

packages/typeorm/src/util/OrmUtils.ts[580-613]
packages/typeorm/src/persistence/SubjectChangedColumnsComputer.ts[167-185]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`compareOwnKeys` can accept a non-enumerable property as a match for an enumerable key, even when the other object has a different enumerable key.
## Fix Focus Areas
- packages/typeorm/src/util/OrmUtils.ts[586-597]
- packages/typeorm/test/unit/util/orm-utils.test.ts[271-304]
## Recommended Fix
Check membership against the other object's enumerable own keys, not all own properties. Add a regression test with equal key counts but different enumerable keys, where a non-enumerable property masks the missing key.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Context sources
Review mode: Auto: ⚖️ Balanced: Runtime comparison semantics and cycle handling introduce real correctness risk.
Results up to commit 6977f6e


🐞 Bugs (1) 📘 Rule violations (0) 📜 Skill insights (0)


Remediation recommended
1. Different object fields compare as equal 🐞 Bug ≡ Correctness
Description
compareOwnKeys counts enumerable keys with Object.keys, but accepts a matching non-enumerable
key through hasOwnProperty, leaving a different enumerable key unchecked. For {a: 1} versus `{b:
2} with an additional non-enumerable a: 1 on the second object, deepCompare` returns true; if
such a value reaches the JSON save comparison, the column update is skipped.
Code

packages/typeorm/src/util/OrmUtils.ts[593]

+            if (!Object.prototype.hasOwnProperty.call(y, key)) return false
Evidence
Object.keys(y) counts only its enumerable b, while hasOwnProperty.call(y, "a") accepts its
non-enumerable a; the loop never checks b. The JSON/JSONB save path returns without recording a
column difference when deepCompare returns true.

packages/typeorm/src/util/OrmUtils.ts[580-613]
packages/typeorm/src/persistence/SubjectChangedColumnsComputer.ts[167-185]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`compareOwnKeys` can accept a non-enumerable property as a match for an enumerable key, even when the other object has a different enumerable key.

## Fix Focus Areas
- packages/typeorm/src/util/OrmUtils.ts[586-597]
- packages/typeorm/test/unit/util/orm-utils.test.ts[271-304]

## Recommended Fix
Check membership against the other object's enumerable own keys, not all own properties. Add a regression test with equal key counts but different enumerable keys, where a non-enumerable property masks the missing key.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Context sources
Review mode: Auto: ⚖️ Balanced: Core comparison semantics and cycle handling carry meaningful behavioral risk.

Grey Divider

Qodo Logo

@pkg-pr-new

pkg-pr-new Bot commented Oct 24, 2026

Copy link
Copy Markdown
  • typeorm-sql-js-example

    npm i https://pkg.pr.new/typeorm@12999
    

commit: 7243bd2

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

linked-issue PR references an issue performance

Development

Successfully merging this pull request may close these issues.

perf: OrmUtils.deepCompare is 9-14x slower than necessary for json columns and array columns on save

2 participants