Generic deep diff between two objects

Generic deep diff between two objects

I have two objects: oldObj and newObj.
The data in oldObj was used to populate a form and newObj is the result of the user changing data in this form and submitting it.
Both objects are deep, ie. they have properties that are objects or arrays of objects etc – they can be n levels deep, thus the diff algorithm needs to be recursive.
Now I need to not just figure out what was changed (as in added/updated/deleted) from oldObj to newObj, but also how to best represent it.
So far my thoughts was to just build a genericDeepDiffBetweenObjects method that would return an object on the form {add:{…},upd:{…},del:{…}} but then I thought: somebody else must have needed this before.
So… does anyone know of a library or a piece of code that will do this and maybe have an even better way of representing the difference (in a way that is still JSON serializable)?
I have thought of a better way to represent the updated data, by using the same object structure as newObj, but turning all property values into objects on the form:
{type: ‘‘, data: }

So if newObj.prop1 = ‘new value’ and oldObj.prop1 = ‘old value’ it would set returnObj.prop1 = {type: ‘update’, data: ‘new value’}
Update 2:
It gets truely hairy when we get to properties that are arrays, since the array [1,2,3] should be counted as equal to [2,3,1], which is simple enough for arrays of value based types like string, int & bool, but gets really difficult to handle when it comes to arrays of reference types like objects and arrays.
Example arrays that should be found equal:
[1,[{c: 1},2,3],{a:’hey’}] and [{a:’hey’},1,[3,{c: 1},2]]

Not only is it quite complex to check for this type of deep value equality, but also to figure out a good way to represent the changes that might be.

Related:  How to structure a single page app with knockout.js?


Solution 1:

I wrote a little class that is doing what you want, you can test it here.

Only thing that is different from your proposal is that I don’t consider [1,[{c: 1},2,3],{a:'hey'}] and [{a:'hey'},1,[3,{c: 1},2]] to be same, because I think that arrays are not equal if order of their elements is not same. Of course this can be changed if needed. Also this code can be further enhanced to take function as argument that will be used to format diff object in arbitrary way based on passed primitive values (now this job is done by “compareValues” method).

var deepDiffMapper = function () {
  return {
    VALUE_CREATED: 'created',
    VALUE_UPDATED: 'updated',
    VALUE_DELETED: 'deleted',
    VALUE_UNCHANGED: 'unchanged',
    map: function(obj1, obj2) {
      if (this.isFunction(obj1) || this.isFunction(obj2)) {
        throw 'Invalid argument. Function given, object expected.';
      if (this.isValue(obj1) || this.isValue(obj2)) {
        return {
          type: this.compareValues(obj1, obj2),
          data: obj1 === undefined ? obj2 : obj1

      var diff = {};
      for (var key in obj1) {
        if (this.isFunction(obj1[key])) {

        var value2 = undefined;
        if (obj2[key] !== undefined) {
          value2 = obj2[key];

        diff[key] =[key], value2);
      for (var key in obj2) {
        if (this.isFunction(obj2[key]) || diff[key] !== undefined) {

        diff[key] =, obj2[key]);

      return diff;

    compareValues: function (value1, value2) {
      if (value1 === value2) {
        return this.VALUE_UNCHANGED;
      if (this.isDate(value1) && this.isDate(value2) && value1.getTime() === value2.getTime()) {
        return this.VALUE_UNCHANGED;
      if (value1 === undefined) {
        return this.VALUE_CREATED;
      if (value2 === undefined) {
        return this.VALUE_DELETED;
      return this.VALUE_UPDATED;
    isFunction: function (x) {
      return === '[object Function]';
    isArray: function (x) {
      return === '[object Array]';
    isDate: function (x) {
      return === '[object Date]';
    isObject: function (x) {
      return === '[object Object]';
    isValue: function (x) {
      return !this.isObject(x) && !this.isArray(x);

var result ={
  a: 'i am unchanged',
  b: 'i am deleted',
  e: {
    a: 1,
    b: false,
    c: null
  f: [1, {
    a: 'same',
    b: [{
      a: 'same'
    }, {
      d: 'delete'
  g: new Date('2017.11.25')
}, {
  a: 'i am unchanged',
  c: 'i am created',
  e: {
    a: '1',
    b: '',
    d: 'created'
  f: [{
    a: 'same',
    b: [{
      a: 'same'
    }, {
      c: 'create'
  }, 1],
  g: new Date('2017.11.25')

Solution 2:

Using Underscore, a simple diff:

Related:  How to assign alternate class to rows in Angular JS?
var o1 = {a: 1, b: 2, c: 2},
    o2 = {a: 2, b: 1, c: 2};

_.omit(o1, function(v,k) { return o2[k] === v; })

Results in the parts of o1 that correspond but with different values in o2:

{a: 1, b: 2}

It’d be different for a deep diff:

function diff(a,b) {
    var r = {};
    _.each(a, function(v,k) {
        if(b[k] === v) return;
        // but what if it returns an empty object? still attach?
        r[k] = _.isObject(v)
                ? _.diff(v, b[k])
                : v
    return r;

As pointed out by @Juhana in the comments, the above is only a diff a–>b and not reversible (meaning extra properties in b would be ignored). Use instead a–>b–>a:

(function(_) {
  function deepDiff(a, b, r) {
    _.each(a, function(v, k) {
      // already checked this or equal...
      if (r.hasOwnProperty(k) || b[k] === v) return;
      // but what if it returns an empty object? still attach?
      r[k] = _.isObject(v) ? _.diff(v, b[k]) : v;

  /* the function */
    diff: function(a, b) {
      var r = {};
      deepDiff(a, b, r);
      deepDiff(b, a, r);
      return r;

See for full example+tests+mixins

Solution 3:

I’d like to offer an ES6 solution…This is a one-way diff, meaning that it will return keys/values from o2 that are not identical to their counterparts in o1:

let o1 = {
  one: 1,
  two: 2,
  three: 3

let o2 = {
  two: 2,
  three: 3,
  four: 4

let diff = Object.keys(o2).reduce((diff, key) => {
  if (o1[key] === o2[key]) return diff
  return {
    [key]: o2[key]
}, {})

Solution 4:

Using Lodash:

_.mergeWith(oldObj, newObj, function (objectValue, sourceValue, key, object, source) {
    if ( !(_.isEqual(objectValue, sourceValue)) && (Object(objectValue) !== objectValue)) {
        console.log(key + "\n    Expected: " + sourceValue + "\n    Actual: " + objectValue);

I don’t use key/object/source but I left it in there if you need to access them.
The object comparison just prevents the console from printing the differences to the console from the outermost element to the innermost element.

Related:  How to pass parameters to a JQuery $.getJSON callback method?

You can add some logic inside to handle arrays. Perhaps sort the arrays first. This is a very flexible solution.


Changed from _.merge to _.mergeWith due to lodash update. Thanks Aviron for noticing the change.

Solution 5:

These days, there are quite a few modules available for this. I recently wrote a module to do this, because I wasn’t satisfied with the numerous diffing modules I found. Its called odiff: . I also listed a bunch of the most popular modules and why they weren’t acceptable in the readme of odiff, which you could take a look through if odiff doesn’t have the properties you want. Here’s an example:

var a = [{a:1,b:2,c:3},              {x:1,y: 2, z:3},              {w:9,q:8,r:7}]
var b = [{a:1,b:2,c:3},{t:4,y:5,u:6},{x:1,y:'3',z:3},{t:9,y:9,u:9},{w:9,q:8,r:7}]

var diffs = odiff(a,b)

/* diffs now contains:
[{type: 'add', path:[], index: 2, vals: [{t:9,y:9,u:9}]},
 {type: 'set', path:[1,'y'], val: '3'},
 {type: 'add', path:[], index: 1, vals: [{t:4,y:5,u:6}]}

Solution 6:

Here is a JavaScript library which you can use for finding diff between two JavaScript object :

Github url:

Npmjs url:


You can use recursive-diff library in browser as well as Node.js. For browser, do the following:

<script type="text" src="index.js"/>
<script type="text/javascript">
     var ob1 = {a:1, b: [2,3]};
     var ob2 = {a:2, b: [3,3,1]};
     var delta = diff.getDiff(ob1,ob2); 
     /* console.log(delta) will dump following data 
     } */
     var ob3 = diff.applyDiff(ob1, delta); //expect ob3 is deep equal to ob2

Whereas in node.js you can require ‘recursive-diff’ module and use it like below:

var diff = require('recursive-diff');
var ob1 = {a: 1}, ob2: {b:2};
var diff = diff.getDiff(ob1, ob2);