Iterating over all object entries. for vs for-in vs for-of

Benchmark created on


Description

Case of a small object, 7 keys

Setup

const obj = {
	a: 0,
	b: 2, 
	c: 3,
	d: 4,
	e: 5,
	f: 6,
	g: 7,
};

let counter = 0; // dummy counter that adds minimal overhead but prevents theoretically possible runtime optimization

Teardown

counter = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
for
const entries = Object.entries(obj);
const len = entries.length; 

for (let i = 0; i < len; i++) {
	if (entries[i][1]) {
		counter++;
	}
}
ready
for-in
for (let key in obj) {
	if (obj[key]) {
		counter++;
	}
}
ready
for-of
const entries = Object.entries(obj);

for (let entry of entries) {
	if (entry[1]) {
		counter++;
	}
}
ready

Revisions

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