Object key length read performance

Benchmark created on


Setup

function createObj (keyLength, nKeys) {
	const obj = {};
	for (let i = 0; i < nKeys; i++) {
		const key = randStr(keyLength);
		// Random value, just in the case the engine does some optimizations under the hood
		obj[key] = randStr(32);
	}
	return obj;
}

function randStr(length) {
    let result = '';
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    const charactersLength = characters.length;
    let counter = 0;
    while (counter < length) {
      result += characters.charAt(Math.floor(Math.random() * charactersLength));
      counter += 1;
    }
    return result;
}

function testRead(obj) {
	let sum = 0;
	Object.keys(obj).forEach((key) => {
		sum += obj[key].length;
	});
	return sum;
}

const n = 32;
// Create the objects upfront; the tests are about reading from them, not creation time
let objects = {
	10: createObj(10, n),
	100: createObj(100, n),
	1000: createObj(1000, n),
	10_000: createObj(10_000, n),
	13_750: createObj(13_750, n),
	15_000: createObj(15_000, n),
	16_250: createObj(16_250, n),
	17_500: createObj(17_500, n),
	25_000: createObj(25_000, n),
	50_000: createObj(50_000, n),
	100_000: createObj(100_000, n),
	// These make Chrome very sad
	// 1_000_000: createObj(1_000_000, n),
	// 10_000_000: createObj(10_000_000, n)
}

Test runner

Ready to run.

Testing in
TestOps/sec
10
testRead(objects[10])
ready
10_000
testRead(objects[10_000])
ready
13_750
testRead(objects[13_750])
ready
15_000
testRead(objects[15_000])
ready
16_250
testRead(objects[16_250])
ready
17_500
testRead(objects[17_500])
ready
25_000
testRead(objects[25_000])
ready
50_000
testRead(objects[50_000])
ready
100_000
testRead(objects[100_000])
ready

Revisions

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