getOrInsert() (v2)

Revision 2 of this benchmark created on


Setup

Map.prototype.getOrInsertComputed = function getOrInsertComputed(key, value) {
	if (!this.has(key)) {
		this.set(key, value());
	}
	return this.get(key);
}

Test runner

Ready to run.

Testing in
TestOps/sec
raw
const map = new Map();
for (let i = 0; i < 10; ++i) {
	const k = Math.floor(i % 10);
	const v = Math.random();
	let existing = map.get(k);
	if (!existing) {
		existing = [];
	}
	existing.push(v);
}
ready
proposal-polyfill
const map = new Map();
for (let i = 0; i < 10; ++i) {
	const k = Math.floor(i % 10);
	const v = Math.random();
	map.getOrInsertComputed(k, () => []).push(v);
}
ready

Revisions

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