Skip to content
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

Various cache read and write performance optimizations. #5948

Merged
merged 11 commits into from
Feb 16, 2020
Merged
Next Next commit
Use Set for DeepMerger pastCopies instead of array/indexOf.
When I wrote this code, I thought this array would usually be so short
that indexOf would be faster than Set.prototype.has, but of course the
pathological cases are what end up mattering, and I've recently seen some
result objects that cause thousands of shallow copies to be made over a
series of many merges using one DeepMerger instance.
  • Loading branch information
benjamn committed Feb 15, 2020
commit d6edbaef76aa1dedf5a146e110a79141bc7dd3ea
12 changes: 4 additions & 8 deletions src/utilities/common/mergeDeep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ const defaultReconciler: ReconcilerFunction<any[]> =
};

export class DeepMerger<TContextArgs extends any[]> {
private pastCopies: any[] = [];

constructor(
private reconciler: ReconcilerFunction<TContextArgs> = defaultReconciler,
) {}
Expand Down Expand Up @@ -101,12 +99,10 @@ export class DeepMerger<TContextArgs extends any[]> {

public isObject = isObject;

private pastCopies = new Set<any>();

public shallowCopyForMerge<T>(value: T): T {
if (
value !== null &&
typeof value === 'object' &&
this.pastCopies.indexOf(value) < 0
) {
if (isObject(value) && !this.pastCopies.has(value)) {
if (Array.isArray(value)) {
value = (value as any).slice(0);
} else {
Expand All @@ -115,7 +111,7 @@ export class DeepMerger<TContextArgs extends any[]> {
...value,
};
}
this.pastCopies.push(value);
this.pastCopies.add(value);
}
return value;
}
Expand Down