has+get vs get

Benchmark created on


Setup

const items = []

for (let i = 0; i<1000; i++) {
	items.push([1,{requestedId:1, replaceWithVirtualId: i%2==0}])
}
const testedId = 243;

Test runner

Ready to run.

Testing in
TestOps/sec
has + get
const map = new Map();
for (const [key,value] of items) {
	map.set(key, value)
}

if (map.has(testedId)) {
    const value = map.get(testedId);
    console.log(value)
} else {
    console.log(undefined)
}
ready
only get
const map = new Map();
for (const [key,value] of items) {
	map.set(key, value)
}

const value = map.get(testedId);
if(value) {    
	console.log(value)
} else {
    console.log(undefined)
}
ready
array
const array = [];
for (const [key,value] of items) {
	array.push({...value, originalId: key})
}

const value = array.find(a=>a.originalId === testedId)
if(value) {    
	console.log(value)
} else {
    console.log(undefined)
}
ready

Revisions

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