Skip to content

Commit

Permalink
refactor(core): optimize reducers performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Nov 16, 2024
1 parent 0d57e1a commit eebb4ce
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
15 changes: 6 additions & 9 deletions packages/core/src/utils/objects/cleanObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import {isProtectedKey} from "./isProtectedKey.js";
* @param obj
* @param ignore
*/
export function cleanObject(obj: any, ignore: string[] = []): any {
export function cleanObject(obj: Record<string, unknown>, ignore: string[] = []): any {
return Object.entries(obj).reduce((obj, [key, value]) => {
if (isProtectedKey(key) || ignore.includes(key)) {
if (isProtectedKey(key) || ignore.includes(key) || value === undefined) {
return obj;
}

return value === undefined
? obj
: {
...obj,
[key]: value
};
}, {});
obj[key] = value;

return obj
}, {} as Record<string, unknown>);
}
11 changes: 6 additions & 5 deletions packages/core/src/utils/objects/deepMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export function deepMerge<T = any, C = any>(source: T & any, obj: C & any, optio
return [].concat(obj).reduce((out: any[], value: any) => reducer(out, value, options), [...source]);
}

const newObj = createInstance(source);

return [...objectKeys(source), ...objectKeys(obj)].reduce((out: any, key: string) => {
const src = source && source[key];
const value = deepMerge(src, obj && obj[key], {
Expand All @@ -75,9 +77,8 @@ export function deepMerge<T = any, C = any>(source: T & any, obj: C & any, optio
return out;
}

return {
...out,
[key]: value
};
}, createInstance(source));
out[key] = value;

return out;
}, newObj);
}

0 comments on commit eebb4ce

Please sign in to comment.