var arr = [];
for (var i = 0; i < 1000; i++) {
arr[i] = { data: i };
}
function someFn(i) {
return i.data * 3 * 8;
}
arr.forEach(function (item){
someFn(item);
})
for (var i = 0, len = arr.length; i < len; i++) {
someFn(arr[i]);
}
arr.map(item => someFn(item))
for (const el of arr) {
someFn(el);
}
let f=someFn(arr[0]);
let rest=arr.slice(1).map(el => (someFn(el)));
let result = [f, rest];
let result = [someFn(arr[0])];
for (let i=1; i<arr.length; i++) {
result[i] = someFn(arr[i]);
}
let result = [someFn(arr[0])];
for (let i=1; i<arr.length; i++) {
result.push(someFn(arr[i]));
}
let result = [someFn(arr[0]), arr.slice(1).map(el => (someFn(el)))];
let rest=arr.slice(1).map(el => (someFn(el)));
let result = [someFn(arr[0]), rest];
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
foreach | |
for | |
map | |
for..of | |
slice + [x, ...y] | |
for (no slice) | |
for + push | |
slice + [x, ...y] v2 | |
slice + [x, ...y] v3 |
Test name | Executions per second |
---|---|
foreach | 23487.2 Ops/sec |
for | 11938.3 Ops/sec |
map | 22811.2 Ops/sec |
for..of | 21701.9 Ops/sec |
slice + [x, ...y] | 18631.7 Ops/sec |
for (no slice) | 7851.7 Ops/sec |
for + push | 7491.2 Ops/sec |
slice + [x, ...y] v2 | 18574.8 Ops/sec |
slice + [x, ...y] v3 | 18667.1 Ops/sec |
Let's break down the provided JSON benchmark definition and test cases to understand what is being tested.
Benchmark Definition
The benchmark defines a script that creates an array of 1000 objects, each with a data
property, and then uses various loop constructs (forEach, for, map, for..of) to iterate over this array and apply a function (someFn
) to each element. The purpose of this benchmark is to compare the performance of different loop constructs in JavaScript.
Options Compared
The options being compared are:
arr.forEach(function (item){\r\n someFn(item);\r\n})
: Uses the forEach
method to iterate over the array.for (var i = 0, len = arr.length; i < len; i++) {\r\n someFn(arr[i]);\r\n}
: Uses a traditional for loop to iterate over the array.arr.map(item => someFn(item))
: Uses the map
method to create a new array with the transformed elements.for (const el of arr) {\r\n someFn(el);\r\n}
: Uses a for..of loop to iterate over the array.slice + [x, ...y]
: These are used to split the array into chunks and then process each chunk.Performance Comparison
The benchmark measures the execution time of each option in different browsers (in this case, only Opera 112). The results show that:
for..of
loop is the fastestmap
method is slower than the for loopsforEach
method is slower than both for loops and mapslice + [x, ...y]
are slow due to their use of array slicingTakeaways
This benchmark highlights the importance of choosing the right loop construct for a specific problem. In this case, the for..of loop provides the best performance, while the map
method and forEach
method may be slower due to their overhead.
It's also worth noting that the use of array slicing in the slice + [x, ...y]
variations can lead to poor performance, as it creates new arrays and involves additional copies of the data.