Skip to content

Commit

Permalink
perf(diff): add fail fasts
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 2, 2018
1 parent 9848576 commit 448e839
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/diff/src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export function diffArray(_a, _b) {
const: {},
linear: []
};
if (_a === _b) {
return state;
}
const reverse = _a.length >= _b.length,
adds = state[reverse ? "dels" : "adds"],
dels = state[reverse ? "adds" : "dels"],
Expand Down
13 changes: 8 additions & 5 deletions packages/diff/src/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ export function diffObject(a: any, b: any) {
const adds = [],
dels = [],
edits = [],
keys = new Set(Object.keys(a).concat(Object.keys(b)));
let distance = 0;
keys = new Set(Object.keys(a).concat(Object.keys(b))),
state = <ObjectDiff>{ distance: 0, adds, dels, edits };
if (a === b) {
return state;
}
for (let k of keys) {
const va = a[k],
vb = b[k],
hasA = va !== undefined;
if (hasA && vb !== undefined) {
if (!equiv(va, vb)) {
edits.push([k, vb]);
distance++;
state.distance++;
}
} else {
(hasA ? dels : adds).push(k);
distance++;
state.distance++;
}
}
return <ObjectDiff>{ distance, adds, dels, edits };
return state;
}

0 comments on commit 448e839

Please sign in to comment.