Map iteration (object vs map) when you need keys only (v5)

Revision 5 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 arr = [];

Test runner

Ready to run.

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

Revisions

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