Iteration over all object values (v3)

Revision 3 of this benchmark created on


Description

Given an object "obj" with lots of properties, compare different solutions to iterate over each value to operate on them.

Setup

const obj = {};
const arr = [];
for (let i = 0; i < 10000; ++i) {
	obj['property-' + i] = i;
	arr.push(i);
}
function operate(x) {
	if (x % 2) {
		return ++x;
	}
	return --x;
}

Test runner

Ready to run.

Testing in
TestOps/sec
Using Object.values().forEach()
Object.values(obj).forEach(num => operate(num));
ready
Using for...in loop
for (let num in obj) {
	operate(num);
}
ready
Using Array.forEach()
arr.forEach(num => operate(num));
ready
Using Array for loop
for (let i = 0; i < arr.length; ++i) {
	operate(arr[i]);
}
ready

Revisions

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