[Perf] deepMerge, deepDiff: Optimize with __deleteAllChildren
This commit is contained in:
parent
7430336ba7
commit
aac2ee85df
@ -1,8 +1,10 @@
|
||||
import { unique } from './iteratees';
|
||||
|
||||
const EQUAL = Symbol('EQUAL');
|
||||
const DELETE = { __delete: true };
|
||||
const DELETE_ALL_CHILDREN = { __deleteAllChildren: true };
|
||||
|
||||
export function deepDiff<T extends any>(value1: T, value2: T): Partial<T> | typeof EQUAL {
|
||||
export function deepDiff<T extends any>(value1: T, value2: T): Partial<T> | typeof EQUAL | typeof DELETE_ALL_CHILDREN {
|
||||
const type1 = typeof value1;
|
||||
const type2 = typeof value2;
|
||||
|
||||
@ -26,7 +28,14 @@ export function deepDiff<T extends any>(value1: T, value2: T): Partial<T> | type
|
||||
|
||||
const object1 = value1 as AnyLiteral;
|
||||
const object2 = value2 as AnyLiteral;
|
||||
const allKeys = unique(Object.keys(object1).concat(Object.keys(object2)));
|
||||
const keys1 = Object.keys(object1);
|
||||
const keys2 = Object.keys(object2);
|
||||
|
||||
if (!keys2.length) {
|
||||
return !keys1.length ? EQUAL : DELETE_ALL_CHILDREN;
|
||||
}
|
||||
|
||||
const allKeys = unique(keys1.concat(keys2));
|
||||
|
||||
const diff = allKeys.reduce((acc: any, key) => {
|
||||
if (object1[key] === object2[key]) {
|
||||
@ -36,7 +45,7 @@ export function deepDiff<T extends any>(value1: T, value2: T): Partial<T> | type
|
||||
const o1has = object1.hasOwnProperty(key);
|
||||
const o2has = object2.hasOwnProperty(key);
|
||||
if (!o2has) {
|
||||
acc[key] = { __delete: true };
|
||||
acc[key] = DELETE;
|
||||
return acc;
|
||||
}
|
||||
if (!o1has && o2has) {
|
||||
|
||||
@ -21,6 +21,12 @@ export function deepMerge<T extends any>(value1: T, value2: Record<keyof T, any>
|
||||
|
||||
const object1 = value1 as AnyLiteral;
|
||||
const object2 = value2 as AnyLiteral;
|
||||
|
||||
// eslint-disable-next-line no-underscore-dangle
|
||||
if (object2.__deleteAllChildren) {
|
||||
return {} as T;
|
||||
}
|
||||
|
||||
const allKeys = unique(Object.keys(object1).concat(Object.keys(object2)));
|
||||
|
||||
return allKeys.reduce((acc: AnyLiteral, key) => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user