const array = [];
for (let i = 0; i < 1000000; ++i) {
result.push({
a: i,
b: i / 2,
r: 0,
});
}
array.forEach((x) => {
x.r = x.a + x.b;
});
for(const x of array) {
x.r = x.a + x.b;
}
const r = [];
for(const {a, b} of array) {
r.push(a + b);
}
array.map(x => x.a + x.b)
array.map(({a,b}) => a + b)
const r = new Array(array.length);
for (let i = 0; i < array.length; ++i) {
r[i] = array[i].a + array[i].b;
}
array.reduce((p, x) => p + x.a + x.b, 0);
array.reduce((p, {a,b}) => p + a + b, 0);
let r = 0;
for (const x of array) {
r += x.a + x.b;
}
let r = 0;
for (const {a,b} of array) {
r += a + b;
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
.forEach | |
for..of | |
for..of (destructuring) | |
.map | |
.map (destructuring) | |
.for (init array) | |
.reduce | |
.reduce (destructuring) | |
for..of (reduce) | |
for..of (reduce) (destructuring) |
Test name | Executions per second |
---|---|
.forEach | 41316172.0 Ops/sec |
for..of | 62540452.0 Ops/sec |
for..of (destructuring) | 63171200.0 Ops/sec |
.map | 36072416.0 Ops/sec |
.map (destructuring) | 35287688.0 Ops/sec |
.for (init array) | 56116088.0 Ops/sec |
.reduce | 49730352.0 Ops/sec |
.reduce (destructuring) | 54160524.0 Ops/sec |
for..of (reduce) | 63571516.0 Ops/sec |
for..of (reduce) (destructuring) | 63874252.0 Ops/sec |
The benchmark defined on MeasureThat.net evaluates the performance of various methods and iterations for manipulating and processing arrays in JavaScript, specifically focusing on techniques for summing properties of objects within an array. The benchmark prepares a large array of objects (1,000,000 in total), each containing properties a
, b
, and r
.
The benchmark includes different approaches to iterate over the array and calculate the sum of properties a
and b
:
.forEach: Uses the forEach
method to apply a function on each element of the array. It modifies the property r
of each object in the array.
for..of: Utilizes the for..of
loop to iterate over the array.
for..of (destructuring): Similar to the standard for..of
, but uses destructuring to directly access properties a
and b
.
.map: Applies the map
function to transform the array, returning a new array composed of the sums of a
and b
.
.map (destructuring): Uses map
with destructuring to simplify access to properties.
map
..for (init array): A standard for
loop with pre-initialized results. It explicitly initializes an array for storing results.
.reduce: The reduce
method is employed to accumulate values, summing a
and b
while iterating.
.reduce (destructuring): Similar to regular reduce
, utilizing destructuring for clarity.
for..of (reduce): A for..of
loop that mimics the functionality of reduce
.
reduce
, can be less elegant.for..of (reduce) (destructuring): Combines the for..of
loop with destructuring for clarity in summation.
for..of
and destructuring, offering both clarity and efficiency.The latest benchmark results indicate that the top-performing methods were the for..of
loops, especially when destructuring was used. Here are some performance insights:
for..of (reduce) (destructuring)
for..of (reduce)
for..of (destructuring)
All these approaches showcased superior execution rates compared to methods like .forEach
, .map
, and even .reduce
without destructuring, demonstrating the efficiency of iteration and its direct influence on performance.
When selecting an approach:
.map
, .forEach
) tend to provide cleaner code, which can be more maintainable. However, in performance-critical applications, traditional loops (like for
or for..of
) may yield better results.map
) can introduce additional memory overhead, thus affecting performance.In addition to the tested approaches, developers can also consider:
Array.prototype.filter
for operations that involve conditional logic for inclusion in new arrays.Array.prototype.flatMap
for cases that require mapping and flattening of arrays simultaneously.Overall, the choice of technique should balance performance needs against code maintainability and readability in the context of the application's requirements.