title: Javascript: Deep Comparison
body: <p>I was checking this question <a href="https://stackoverflow.com/questions/36783655/javascript-deep-comparison">Javascript Deep Comparison</a>
  The solution of the question asker did not convince me so I tried to analyze the problem and came up with that</p>
  
  <pre><code>var obj = {here: 2};
  console.log(deepEqual(obj, obj));
  // → true
  console.log(deepEqual(obj, {here: 1}));
  // → false
  console.log(deepEqual(obj, {here: 2}));
  // → true
  function deepEqual(a,b)
  {
    if( (typeof a == 'object' &amp;&amp; a != null) &amp;&amp;
        (typeof b == 'object' &amp;&amp; b != null) )
    {
       var count = [0,0];
       for( var key in a) count[0]++;
       for( var key in b) count[1]++;
       if( count[0]-count[1] != 0) {console.log('1');return false;}
       for( var key in a)
       {
         if(!(key in b) || !deepEqual(a[key],b[key])) {console.log('2');return false;}
       }
       for( var key in b)
       {
         if(!(key in a) || !deepEqual(b[key],a[key])) {console.log('3');return false;}
       }  
    }
    console.log('a:'+a+' b:'+b);
    return a===b;
  }
  obj === { here:2 }
  </code></pre>
  
  <p>This code fails the last test ( console.log(deepEqual(obj, {here: 2})) ) but the logic of considering objects deeply equal if they have respectively equal keys and values despite being different instances in memory does not convince me. Is there's a problem with my 'solution' or the mistake lies in the presumptions of the exercise? Is the code mentioned in the question I linked valid? </p>
  
  <p>Resources that hikinthru forgot to mention ( <a href="http://eloquentjavascript.net/04_data.html#exercise_deep_compare" rel="noreferrer">http://eloquentjavascript.net/04_data.html#exercise_deep_compare</a> )</p>