Map iteration (object vs map) when you need keys and values (v7)

Revision 7 of this benchmark created on


Setup

const obj = {};
for (let i=0; i<1e3; i++) {
	obj[Math.random().toString()] = Math.random();
}
function* generatorEntries(x) {
	for (const key of Object.keys(x)) {
		yield [key, x[key]];
	}
}
const map = new Map(Object.entries(obj));
let sum = 0;
let arr = [];

Test runner

Ready to run.

Testing in
TestOps/sec
for...in
for (const key in obj) {
	arr.push(key)
	sum += obj[key];
}
ready
for...of Object.keys
for (const key of Object.keys(obj)) {
	arr.push(key)
	sum += obj[key];
}
ready
Object.keys().forEach
Object.keys(obj).forEach(key => {
	arr.push(key);
	sum += obj[key];
})
ready
for...of Object.entries
for (const [key, v] of Object.entries(obj)) {
	arr.push(key)
	sum += v;
}
ready
for...of map (entries)
for (const [key, v] of map) {
	arr.push(key);
	sum += v;
}
ready
for...of map.keys()
for (const key of map.keys()) {
	arr.push(key);
	sum += map.get(key);
}
ready
for...of generator entries
for (const [key, v] of generatorEntries(obj)) {
	arr.push(key)
	sum += v;
}
ready

Revisions

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