title: Generic deep diff between two objects
body: <p>I have two objects: <code>oldObj</code> and <code>newObj</code>.</p>
  <p>The data in <code>oldObj</code> was used to populate a form and <code>newObj</code> is the result of the user changing data in this form and submitting it.</p>
  <p>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.</p>
  <p>Now I need to not just figure out what was changed (as in added/updated/deleted) from <code>oldObj</code> to <code>newObj</code>, but also how to best represent it.</p>
  <p>So far my thoughts was to just build a <code>genericDeepDiffBetweenObjects</code> method that would return an object on the form <code>{add:{...},upd:{...},del:{...}}</code> but then I thought: somebody else must have needed this before.</p>
  <p>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)?</p>
  <h3>Update:</h3>
  <p>I have thought of a better way to represent the updated data, by using the same object structure as <code>newObj</code>, but turning all property values into objects on the form:</p>
  <pre><code>{type: '&lt;update|create|delete&gt;', data: &lt;propertyValue&gt;}
  </code></pre>
  <p>So if <code>newObj.prop1 = 'new value'</code> and <code>oldObj.prop1 = 'old value'</code> it would set <code>returnObj.prop1 = {type: 'update', data: 'new value'}</code></p>
  <h3>Update 2:</h3>
  <p>It gets truely hairy when we get to properties that are arrays, since the array <code>[1,2,3]</code> should be counted as equal to <code>[2,3,1]</code>, which is simple enough for arrays of value based types like string, int &amp; bool, but gets really difficult to handle when it comes to arrays of reference types like objects and arrays.</p>
  <p>Example arrays that should be found equal:</p>
  <pre><code>[1,[{c: 1},2,3],{a:'hey'}] and [{a:'hey'},1,[3,{c: 1},2]]
  </code></pre>
  <p>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.</p>