Skip to content

Instantly share code, notes, and snippets.

@kirilloid
Created January 16, 2014 19:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirilloid/8461841 to your computer and use it in GitHub Desktop.
Save kirilloid/8461841 to your computer and use it in GitHub Desktop.
simple object diff: less than 300 bytes compressed
(function diff(a,b) {
var c = JSON.stringify,
i = function (x) {return x==null||typeof x!='object'};
return (function d(a, b, p) {
var s, k, t='';
if (i(a)||i(b))
return a !==b ? p+'-'+c(b)+p+'+'+c(a) : '';
for (k in a)
t += (k in b)
? ((s = d(a[k], b[k], p+' ')) ? p+k+':'+s : '')
: p+'+'+k+':'+c(a[k])
for (k in b)
t += (k in a) ? '' : p+'-'+k+':'+c(b[k]);
return t
}(a,b,'\n'))
}({a:1,b:2,c:3,d:4},{a:1,b:{c:3},c:3}))
/*"
b:
-{"c":3}
+2
+d:4"*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment