array vs set (v5)

Revision 5 of this benchmark created on


Setup

const rand = (size) => [...Array(size)].map(() => Math.floor(Math.random() * size * 100));

const a1 = rand(10000);
const a2 = rand(10000);

Test runner

Ready to run.

Testing in
TestOps/sec
array
let count = 0;

for (let i = 0; i < a1.length; i++) {
  if (a2.includes(a1[i])) {
  	count++;
  }
}
ready
set
const a2Set = new Set(a2);

let count = 0;

for (let i = 0; i < a1.length; i++) {
  if (a2Set.has(a1[i])) {
  	count++;
  }
}
ready
filter + set
const a2Set = new Set(a2);

let count = a1.filter((el) => a2Set.has(el)).length;
ready
filter + array
let count = a1.filter((el) => a2.includes(el)).length
ready

Revisions

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