Skip to content

Commit

Permalink
remove spread operator in object diffs to increase performance
Browse files Browse the repository at this point in the history
  • Loading branch information
SheneekaW authored and mattphillips committed Jan 25, 2022
1 parent 8c09976 commit 4c855be
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/added.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ const addedDiff = (lhs, rhs) => {

if (isObject(difference) && isEmpty(difference)) return acc;

return { ...acc, [key]: difference };
acc[key] = difference;
return acc;
}

return { ...acc, [key]: r[key] };
acc[key] = r[key];
return acc;
}, {});
};

Expand Down
6 changes: 4 additions & 2 deletions src/deleted.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ const deletedDiff = (lhs, rhs) => {

if (isObject(difference) && isEmpty(difference)) return acc;

return { ...acc, [key]: difference };
acc[key] = difference;
return acc;
}

return { ...acc, [key]: undefined };
acc[key] = undefined;
return acc;
}, {});
};

Expand Down
15 changes: 12 additions & 3 deletions src/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ const diff = (lhs, rhs) => {
const r = rhs;

const deletedValues = Object.keys(l).reduce((acc, key) => {
return hasOwnProperty(r, key) ? acc : { ...acc, [key]: undefined };
if (!hasOwnProperty(r, key)) {
acc[key] = undefined;

}

return acc;
}, {});

if (isDate(l) || isDate(r)) {
Expand All @@ -18,15 +23,19 @@ const diff = (lhs, rhs) => {
}

return Object.keys(r).reduce((acc, key) => {
if (!hasOwnProperty(l, key)) return { ...acc, [key]: r[key] }; // return added r key
if (!hasOwnProperty(l, key)){
acc[key] = r[key]; // return added r key
return acc;
}

const difference = diff(l[key], r[key]);

// If the difference is empty, and the lhs is an empty object or the rhs is not an empty object
if (isEmptyObject(difference) && !isDate(difference) && (isEmptyObject(l[key]) || !isEmptyObject(r[key])))
return acc; // return no diff

return { ...acc, [key]: difference }; // return updated key
acc[key] = difference // return updated key
return acc; // return updated key
}, deletedValues);
};

Expand Down
3 changes: 2 additions & 1 deletion src/updated.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ const updatedDiff = (lhs, rhs) => {
if (isEmptyObject(difference) && !isDate(difference) && (isEmptyObject(l[key]) || !isEmptyObject(r[key])))
return acc; // return no diff

return { ...acc, [key]: difference };
acc[key] = difference;
return acc;
}

return acc;
Expand Down

0 comments on commit 4c855be

Please sign in to comment.