Skip to content

Commit e6029db

Browse files
committed
refactor(query-core): improve replaceEqualDeep performance
1 parent b998f68 commit e6029db

File tree

1 file changed

+28
-27
lines changed

1 file changed

+28
-27
lines changed

packages/query-core/src/utils.ts

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -257,38 +257,39 @@ export function replaceEqualDeep(a: any, b: any): any {
257257
}
258258

259259
const array = isPlainArray(a) && isPlainArray(b)
260-
261-
if (array || (isPlainObject(a) && isPlainObject(b))) {
262-
const aItems = array ? a : Object.keys(a)
263-
const aSize = aItems.length
264-
const bItems = array ? b : Object.keys(b)
265-
const bSize = bItems.length
266-
const copy: any = array ? [] : {}
267-
const aItemsSet = new Set(aItems)
268-
269-
let equalItems = 0
270-
271-
for (let i = 0; i < bSize; i++) {
272-
const key = array ? i : bItems[i]
273-
if (
274-
((!array && aItemsSet.has(key)) || array) &&
275-
a[key] === undefined &&
276-
b[key] === undefined
277-
) {
278-
copy[key] = undefined
260+
const object = !array && isPlainObject(a) && isPlainObject(b)
261+
262+
if (!array && !object) return b
263+
264+
const aItems = array ? a : Object.keys(a)
265+
const aSize = aItems.length
266+
const bItems = array ? b : Object.keys(b)
267+
const bSize = bItems.length
268+
const copy: any = array ? new Array(bSize) : {}
269+
// const aItemsSet = new Set(aItems)
270+
271+
let equalItems = 0
272+
273+
for (let i = 0; i < bSize; i++) {
274+
const key = array ? i : bItems[i]
275+
const aItem = a[key]
276+
if (
277+
(array || a.hasOwnProperty(key)) &&
278+
aItem === undefined &&
279+
b[key] === undefined
280+
) {
281+
copy[key] = undefined
282+
equalItems++
283+
} else {
284+
const value = replaceEqualDeep(aItem, b[key])
285+
copy[key] = value
286+
if (value === aItem && aItem !== undefined) {
279287
equalItems++
280-
} else {
281-
copy[key] = replaceEqualDeep(a[key], b[key])
282-
if (copy[key] === a[key] && a[key] !== undefined) {
283-
equalItems++
284-
}
285288
}
286289
}
287-
288-
return aSize === bSize && equalItems === aSize ? a : copy
289290
}
290291

291-
return b
292+
return aSize === bSize && equalItems === aSize ? a : copy
292293
}
293294

294295
/**

0 commit comments

Comments
 (0)