remove array duplicates

JavaScript performance comparison

Test case created by iolo

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const MAX_RUN = 1000;
  const MAX_LEN = 1000;
  const array = Array.from({length: MAX_LEN}, () => Math.floor(Math.random() * MAX_LEN));
  

};
</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
spread-set
for (let run = 0; run < MAX_RUN; run += MAX_RUN) {
  const set = [...new Set(array)];
}
pending…
array-from-set
for (let run = 0; run < MAX_RUN; run += MAX_RUN) {
  const set = Array.from(new Set(array));
}
pending…
filter-indexOf
for (let run = 0; run < MAX_RUN; run += MAX_RUN) {
  const set = array.filter((item, index) => array.indexOf(item) === index);
}
pending…
reduce-includes-spread
for (let run = 0; run < MAX_RUN; run += MAX_RUN) {
  const set = array.reduce((set, item) => set.includes(item) ? set : [...set, item], []);
}
pending…
reduce-includes-push
for (let run = 0; run < MAX_RUN; run += MAX_RUN) {
  const set = array.reduce((set, item) => { if (!set.includes(item)) { set.push(item); } return set; }, []);
}
pending…
reduce-includes-unshift
for (let run = 0; run < MAX_RUN; run += MAX_RUN) {
  const set = array.reduce((set, item) => { if (!set.includes(item)) { set.unshift(item); } return set; }, []);
}
pending…

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

0 Comments