Test name | Executions per second |
---|---|
for | 165.1 Ops/sec |
foreach | 60.7 Ops/sec |
for..of with reassign | 90.2 Ops/sec |
for..of with no reassign | 41.9 Ops/sec |
function generateTestArray() {
const result = [];
for (let i = 0; i < 1000000; ++i) {
result.push({
a: i,
b: i / 2,
r: 0,
});
}
return result;
}
const array = generateTestArray();
for (let i = 0; i < array.length; i++) {
array[i].r = array[i].a + array[i].b;
}
sum = x => x.r = x.a + x.b;
array.forEach(sum);
for (let x of array) {
x.r = x.a + x.b;
}
const result = [];
for (let x of array) {
result.push(x.a + x.b);
}