Skip to content

Commit 91494ca

Browse files
committed
less code, but still 2x faster
1 parent d225d24 commit 91494ca

1 file changed

Lines changed: 16 additions & 62 deletions

File tree

packages/query-core/src/utils.ts

Lines changed: 16 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -253,92 +253,46 @@ const hasOwn = Object.prototype.hasOwnProperty
253253
* This can be used for structural sharing between JSON values for example.
254254
*/
255255
export function replaceEqualDeep<T>(a: unknown, b: T): T
256-
export function replaceEqualDeep(a: unknown, b: unknown): any {
256+
export function replaceEqualDeep(a: any, b: any): any {
257257
if (a === b) {
258258
return a
259259
}
260260

261-
const aIsArr = isPlainArray(a)
262-
const bIsArr = isPlainArray(b)
263-
264-
// both are arrays
265-
if (aIsArr && bIsArr) {
266-
const aSize = a.length
267-
const bSize = b.length
268-
const copy: Array<unknown> = new Array(bSize)
269-
let equalItems = 0
270-
271-
for (let i = 0; i < bSize; i++) {
272-
const aItem = a[i]
273-
const bItem = b[i]
274-
275-
// most common case (strict equality)
276-
if (aItem === bItem) {
277-
copy[i] = aItem
278-
if (i < aSize) equalItems++
279-
continue
280-
}
281-
282-
// either item is not an array or object
283-
if (
284-
aItem === null ||
285-
bItem === null ||
286-
typeof aItem !== 'object' ||
287-
typeof bItem !== 'object'
288-
) {
289-
copy[i] = bItem
290-
continue
291-
}
292-
293-
const v = replaceEqualDeep(aItem, bItem)
294-
copy[i] = v
295-
if (v === aItem) equalItems++
296-
}
261+
const array = isPlainArray(a) && isPlainArray(b)
297262

298-
return aSize === bSize && equalItems === aSize ? a : copy
299-
}
263+
if (!array && !(isPlainObject(a) && isPlainObject(b))) return b
300264

301-
// only 1 is an array
302-
if (aIsArr || bIsArr) {
303-
return b
304-
}
265+
const aItems = array ? a : Object.keys(a)
266+
const aSize = aItems.length
267+
const bItems = array ? b : Object.keys(b)
268+
const bSize = bItems.length
269+
const copy: any = array ? new Array(bSize) : {}
305270

306-
// at least 1 is not an object
307-
if (!isPlainObject(a) || !isPlainObject(b)) {
308-
return b
309-
}
310-
311-
const aSize = Object.keys(a).length
312-
const copy: Record<PropertyKey, unknown> = {}
313271
let equalItems = 0
314-
let bSize = 0
315-
316-
for (const k in b) {
317-
bSize++
318272

319-
const aItem = a[k]
320-
const bItem = b[k]
273+
for (let i = 0; i < bSize; i++) {
274+
const key: any = array ? i : bItems[i]
275+
const aItem = a[key]
276+
const bItem = b[key]
321277

322-
// most common case (strict equality)
323278
if (aItem === bItem) {
324-
copy[k] = aItem
325-
if (hasOwn.call(a, k)) equalItems++
279+
copy[key] = aItem
280+
if (array ? i < aSize : hasOwn.call(a, key)) equalItems++
326281
continue
327282
}
328283

329-
// either item is not an array or object
330284
if (
331285
aItem === null ||
332286
bItem === null ||
333287
typeof aItem !== 'object' ||
334288
typeof bItem !== 'object'
335289
) {
336-
copy[k] = bItem
290+
copy[key] = bItem
337291
continue
338292
}
339293

340294
const v = replaceEqualDeep(aItem, bItem)
341-
copy[k] = v
295+
copy[key] = v
342296
if (v === aItem) equalItems++
343297
}
344298

0 commit comments

Comments
 (0)