Set vs indexOf for safe insertion

Benchmark created on


Description

Compare the use of a new set to looking for the entry with index of

Setup

const keyToFind = "theMagic";
const startingArray = new Array(20000)
	.fill("something");
const randomIndex = Math.random() * 20000;
startingArray[randomIndex] = keyToFind;

Test runner

Ready to run.

Testing in
TestOps/sec
indexOf
let myIterationArray = [...startingArray];
if(myIterationArray.indexOf(keyToFind) === -1) {
	myIterationArray.push(keyToFind);
} else {
	// return startingArray to maintain ref equality
}
ready
Set
// Always creates a new ref
let myIterationArray = [...new Set([...startingArray, keyToFind])];
ready

Revisions

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