Object.entries forEach vs for in

Benchmark created on


Setup

const input = {}
const output = {}

for (let i = 0; i < 1000; i++) {
	input[i] = Math.random()
}

Test runner

Ready to run.

Testing in
TestOps/sec
Object.entries forEach, no filter
Object.entries(input).forEach(([key, value]) => {
	if (value !== undefined) {
		output[key] = value
	}
})
ready
for in, no filter
for (const key in input) {
	const value = input[key]
	
	if (value !== undefined) {
		output[key] = value
	}
}
ready
Object.entries forEach, with filter
Object.entries(input)
	.filter(([key, value]) => value !== undefined)
	.forEach(([key, value]) => {
		output[key] = value
	})
ready
for in, with filter
const filtered = Object.entries(input)
	.filter(([key, value]) => value !== undefined)
	
for (const key of filtered) {
	const value = filtered[key]
	
	output[key] = value
}
ready

Revisions

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