Check map.has before delete vs straight delete

Benchmark created on


Description

Using js Map object - should we check if the key/val exists before delete?

Setup

const myMap = new Map();

function getRandomString(length) {
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let result = '';
  const charactersLength = characters.length;
  for (let i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() * charactersLength));
  }
  return result;
}

for (let i = 0; i < 1000; i++) {
  myMap.set(i, getRandomString(10));
}
myMap.set('foo', 'bar');

Test runner

Ready to run.

Testing in
TestOps/sec
with check
if (myMap.has('foo')){
	myMap.delete('foo')
}
if (myMap.has('bar')){
	myMap.delete('bar')
}
ready
run delete
myMap.delete('foo')
myMap.delete('bar')
ready

Revisions

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