-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Performance.js
30 lines (28 loc) · 949 Bytes
/
Performance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import _ from 'underscore';
import lodashTransform from 'lodash/transform';
/**
* Deep diff between two objects. Useful for figuring out what changed about an object from one render to the next so
* that state and props updates can be optimized.
*
* @param {Object} object
* @param {Object} base
* @return {Object}
*/
function diffObject(object, base) {
function changes(obj, comparisonObject) {
return lodashTransform(obj, (result, value, key) => {
if (!_.isEqual(value, comparisonObject[key])) {
// eslint-disable-next-line no-param-reassign
result[key] = (
_.isObject(value) && _.isObject(comparisonObject[key]))
? changes(value, comparisonObject[key])
: value;
}
});
}
return changes(object, base);
}
export {
// eslint-disable-next-line import/prefer-default-export
diffObject,
};