Array vs array to set

Benchmark created on


Setup

const arr = [];
for (let i = 0; i < 500; i++) {
	arr.push(Math.random());
}
const preSet = new Set(arr);

Test runner

Ready to run.

Testing in
TestOps/sec
Array only
for (let i = 0; i < arr.length; i++) {
  const idx = Math.floor(Math.random() * arr.length);
  const foundIndex = arr.indexOf(arr[idx]) > -1;
}
ready
Convert to set
const set = new Set(arr);
for (let i = 0; i < arr.length; i++) {
  const idx = Math.floor(Math.random() * arr.length);
  const foundIndex = set.has(arr[idx]);
}
ready

Revisions

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