Skip to content

Commit 4681150

Browse files
author
SheneekaW
committed
remove spread operator in object diffs to increase performance
1 parent 6296889 commit 4681150

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

src/added/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ const addedDiff = (lhs, rhs) => {
1313

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

16-
return { ...acc, [key]: difference };
16+
acc[key] = difference;
17+
return acc;
1718
}
1819

19-
return { ...acc, [key]: r[key] };
20+
acc[key] = r[key];
21+
return acc;
2022
}, {});
2123
};
2224

src/added/index.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('.addedDiff', () => {
1515
['function', () => ({})],
1616
['date', new Date()],
1717
])('returns empty object when given values of type %s are equal', (type, value) => {
18+
console.log('here yub')
1819
expect(addedDiff(value, value)).toEqual({});
1920
});
2021
});

src/deleted/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ const deletedDiff = (lhs, rhs) => {
1212

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

15-
return { ...acc, [key]: difference };
15+
acc[key] = difference;
16+
return acc;
1617
}
1718

18-
return { ...acc, [key]: undefined };
19+
acc[key] = undefined;
20+
return acc;
1921
}, {});
2022
};
2123

src/diff/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ const diff = (lhs, rhs) => {
99
const r = properObject(rhs);
1010

1111
const deletedValues = Object.keys(l).reduce((acc, key) => {
12-
return r.hasOwnProperty(key) ? acc : { ...acc, [key]: undefined };
12+
if (!r.hasOwnProperty(key)) {
13+
acc[key] = undefined;
14+
15+
}
16+
17+
return acc;
1318
}, {});
1419

1520
if (isDate(l) || isDate(r)) {
@@ -18,13 +23,17 @@ const diff = (lhs, rhs) => {
1823
}
1924

2025
return Object.keys(r).reduce((acc, key) => {
21-
if (!l.hasOwnProperty(key)) return { ...acc, [key]: r[key] }; // return added r key
26+
if (!l.hasOwnProperty(key)){
27+
acc[key] = r[key]; // return added r key
28+
return acc;
29+
}
2230

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

2533
if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc; // return no diff
2634

27-
return { ...acc, [key]: difference }; // return updated key
35+
acc[key] = difference // return updated key
36+
return acc; // return updated key
2837
}, deletedValues);
2938
};
3039

src/updated/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const updatedDiff = (lhs, rhs) => {
2121

2222
if (isObject(difference) && isEmpty(difference) && !isDate(difference)) return acc;
2323

24-
return { ...acc, [key]: difference };
24+
acc[key] = difference;
25+
return acc;
2526
}
2627

2728
return acc;

0 commit comments

Comments
 (0)