Always write vs conditional write

Benchmark created on


Description

Testing if it is faster to conditionally update a map or to always update it without a conditional

Setup

// Example for resourceTypes (< 50 typically)
const mapKeys = new Array(50).fill(0).map(() => Math.random().toString());

const mapToTestWith = new Map();

Test runner

Ready to run.

Testing in
TestOps/sec
Check for value before setting
const mapKeyIndex = Math.floor(Math.random() * 50);
const currentSet = mapToTestWith.get(mapKeys[mapKeyIndex]);
if (currentSet) {
	currentSet.add(Math.random());
} else {
	mapToTestWith.set(mapKeys[mapKeyIndex], new Set([Math.random()]));
}
ready
Blindly set always
const mapKeyIndex = Math.floor(Math.random() * 50);
const setOfThings = mapToTestWith.get(mapKeys[mapKeyIndex]) ?? new Set();

setOfThings.add(Math.random());

mapToTestWith.set(mapKeys[mapKeyIndex], setOfThings);
ready

Revisions

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