title: Javascript how to optimise this count array values function
body: <p>I have a function that mimics the <code>array_count_values</code> function from php in javascript but it's not very fast. I'm wondering if there's a way to fix it?</p>
  
  <pre><code>function array_count_values(arr) {
      let a = [], prev;
      arr.sort();
      for ( let i = 0; i &lt; arr.length; i++ ) {
          if ( arr[i] !== prev ) {
              a.push(1)
          } else {
              a[a.length-1]++;
          }
          prev = arr[i];
      }
      return a;
  }
  </code></pre>
  
  <p>It just returns a simple array of numbers with the counts so like <code>2,1,2,1,1</code>. The input in this case would be numeric arrays 5-7 elements long, so for example <code>array_count_values([6,4,10,6,6])</code></p>