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

Revision 6 of this benchmark created on


Setup

const obj = {};
for (let i=0; i<1e3; i++) {
	obj[Math.random().toString()] = Math.random();
}
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

Revisions

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