title: What is the most efficient way to implement GroupBy in Javascript?
body: <p>I am trying to implement a <code>GroupBy</code> method with these parameters</p>
  
  <pre><code>function GroupBy(keySelector, elementSelector, comparer)
  {
      // keySelector = function(e) { return e.ID }
      // elementSelector = function(e) { return e.Name }
      // comparer = { Equals: function(a,b) { return a==b }, GetHashCode:... }
  }
  </code></pre>
  
  <p>However I don't know an efficient way to implement it.</p>
  
  <p><a href="http://jsperf.com/js-groupby-complex" rel="noreferrer">I created a jsPerf test</a> with linq.js and a method I created which doesn't use a comparer and only work on flat types. (<a href="http://jsfiddle.net/BDQ4k/" rel="noreferrer">Output test here</a>)</p>
  
  <p>Other libraries such as underscore and Lo-Dash doesn't take a <code>comparer</code> parameter. So their implementations are irrelevant.</p>
  
  <p>My key could be a class, so I need something to determine if <code>TKey</code> is the same in different instances.</p>
  
  <p>So basically what I am trying to do is copy C# Linq <code>GroupBy</code> behavior <a href="http://msdn.microsoft.com/en-us/library/bb534304%28v=vs.110%29.aspx" rel="noreferrer">documented here</a>.</p>
  
  <p>Sample input:</p>
  
  <pre><code>var arrComplex =
  [
      { N: { Value: 10 }, Name: "Foo" },
      { N: { Value: 10 }, Name: "Bar" },
      { N: { Value: 20 }, Name: "Foo" },
      { N: { Value: 20 }, Name: "Bar" }
  ];
  </code></pre>
  
  <p>Sample output (or something like this):</p>
  
  <pre><code>[
      {
         "Key": {"Value":10},
         "Elements":["Foo","Bar"]
      },
      {
          "Key": {"Value":20},
          "Elements":["Foo","Bar"]
      }
  ] 
  </code></pre>
  
  <p>Any ideas on how to implement it?</p>
  
  <h3>Bounty</h3>
  
  <p>For the bounty I would like you take in consideration that:</p>
  
  <ul>
  <li>The key could be an object</li>
  <li>Two objects can be equal if some property is equal</li>
  <li>It should be as fast as or faster than existing solutions</li>
  <li>The result can be an array or object, doesn't matter as long as I can get elements grouped by a key</li>
  </ul>
  
  <p>Well, I expect a complete answer.</p>