Set vs Filter

JavaScript performance comparison

Revision 4 of this test case created by Wilgert Velinga

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const generateArr = () => {
      const arr = [];
      for (let i = 0; i < 1000; i++) { arr.push(parseInt(Math.random() * 10)); }
      return arr;
  };
  
  const arr = generateArr()

};

Benchmark.prototype.teardown = function() {
  delete arr

};
</script>

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
Using [...new Set()]
const unique = [...new Set(arr)];
pending…
Using Array.protorype.filter()
const filtered = arr.filter((item, index) => arr.indexOf(item) === index);
pending…
Using a traditional for-loop
let result = [];
  for (let i = 0; i < arr.length; i++) {
    if (result.indexOf(arr[i]) === -1) {
      result.push(arr[i]);
    }
  }
pending…

Revisions

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

0 Comments