Skip to content

Instantly share code, notes, and snippets.

@Yimiprod
Last active May 4, 2025 11:59
Show Gist options
  • Save Yimiprod/7ee176597fef230d1451 to your computer and use it in GitHub Desktop.
Save Yimiprod/7ee176597fef230d1451 to your computer and use it in GitHub Desktop.
Deep diff between two object, using lodash
/**
* This code is licensed under the terms of the MIT license
*
* Deep diff between two object, using lodash
* @param {Object} object Object compared
* @param {Object} base Object to compare with
* @return {Object} Return a new object who represent the diff
*/
function difference(object, base) {
function changes(object, base) {
return _.transform(object, function(result, value, key) {
if (!_.isEqual(value, base[key])) {
result[key] = (_.isObject(value) && _.isObject(base[key])) ? changes(value, base[key]) : value;
}
});
}
return changes(object, base);
}
@jfortez
Copy link

jfortez commented Mar 1, 2024

ty

@cawfree
Copy link

cawfree commented Jan 23, 2025

Recently I've been having some success with this one-liner:

const deepDiff = (a, b) => ({});

Not sure if it is just my machine, but I've found it to be significantly faster than any of the alternative recommendations in the thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment