getOrInsert() (v4)

Revision 4 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);
}

var LOOP_COUNT = 100;

Test runner

Ready to run.

Testing in
TestOps/sec
raw
const map = new Map();
for (let i = 0; i < LOOP_COUNT; ++i) {
	const k = Math.floor(Math.random() % 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 < LOOP_COUNT; ++i) {
	const k = Math.floor(Math.random() % 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.