Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

refactor(query-core): improve replaceEqualDeep performance #9604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Sheraff wants to merge 7 commits into TanStack:main
base: main
Choose a base branch
Loading
from Sheraff:refactor-query-core-replace-equal-deep-performance
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor(query-core): improve replaceEqualDeep performance
  • Loading branch information
Sheraff committed Aug 31, 2025
commit e6029dba5b32cda46009db493225f193fa5e296f
55 changes: 28 additions & 27 deletions packages/query-core/src/utils.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -257,38 +257,39 @@ export function replaceEqualDeep(a: any, b: any): any {
}

const array = isPlainArray(a) && isPlainArray(b)

if (array || (isPlainObject(a) && isPlainObject(b))) {
const aItems = array ? a : Object.keys(a)
const aSize = aItems.length
const bItems = array ? b : Object.keys(b)
const bSize = bItems.length
const copy: any = array ? [] : {}
const aItemsSet = new Set(aItems)

let equalItems = 0

for (let i = 0; i < bSize; i++) {
const key = array ? i : bItems[i]
if (
((!array && aItemsSet.has(key)) || array) &&
a[key] === undefined &&
b[key] === undefined
) {
copy[key] = undefined
const object = !array && isPlainObject(a) && isPlainObject(b)

if (!array && !object) return b

const aItems = array ? a : Object.keys(a)
const aSize = aItems.length
const bItems = array ? b : Object.keys(b)
const bSize = bItems.length
const copy: any = array ? new Array(bSize) : {}
// const aItemsSet = new Set(aItems)

let equalItems = 0

for (let i = 0; i < bSize; i++) {
const key = array ? i : bItems[i]
const aItem = a[key]
if (
(array || a.hasOwnProperty(key)) &&
aItem === undefined &&
b[key] === undefined
) {
copy[key] = undefined
equalItems++
} else {
const value = replaceEqualDeep(aItem, b[key])
copy[key] = value
if (value === aItem && aItem !== undefined) {
equalItems++
} else {
copy[key] = replaceEqualDeep(a[key], b[key])
if (copy[key] === a[key] && a[key] !== undefined) {
equalItems++
}
}
}

return aSize === bSize && equalItems === aSize ? a : copy
}

return b
return aSize === bSize && equalItems === aSize ? a : copy
}

/**
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /