array.filter.unique vs new set

JavaScript performance comparison

Revision 27 of this test case created by Dekus

Preparation code

<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
  let array = [];
  
  for (let i = 0; i < 10000; i++) {
    array.push(Math.floor(Math.random() * 10));
  }
  
  function unique(arr) {
  let l = arr.length;
  let ret = [];
  let found = {};
  for (let i = 0; i < l; i++) {
    let item = arr[i];
    if (!found[item]) {
      found[item] = i + 1;
      ret.push(item);
    }
  }
  return ret;
  }
  
  

};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
new set
[...new Set(array)]
pending…
hash filter
unique(array)
pending…
filter
array.filter((v, i, a) => a.indexOf(v) === i)
pending…
lodash unique
_.uniq(array)
pending…
reduce
array.reduce((u, v) => u.includes(v) ? u : [...u, v], [])
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments