var arr = [];
for (var i = 0; i < 100000; i++) {
arr[i] = i;
}
function someFn(i) {
return i * 3 * 8;
}
const res = []
arr.forEach(function (item){
res.push(someFn(item))
})
const res = []
for (var i = 0, len = arr.length; i < len; i++) {
res.push(someFn(arr[i]))
}
const res = []
const len = arr.length;
for (var i = 0; i < len; i++) {
res.push(someFn(arr[i]))
}
const res = arr.map(item => someFn(item))
const res = []
for (const el of arr) {
res.push(someFn(el))
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
1 | |
2 | |
3 | |
4 | |
5 |
Test name | Executions per second |
---|---|
1 | 70.0 Ops/sec |
2 | 57.0 Ops/sec |
3 | 57.4 Ops/sec |
4 | 102.4 Ops/sec |
5 | 102.0 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark measures the performance of different JavaScript approaches for iterating over an array and applying a transformation function.
Script Preparation Code
The script preparation code defines an array arr
with 100,000 elements and a function someFn
that takes an integer i
as input and returns the result of multiplying i
by 3 and 8. The script also initializes an empty array res
that will store the transformed values.
Html Preparation Code
The html preparation code is null, indicating that no HTML element is involved in this benchmark.
Benchmark Definition
The benchmark definition consists of five test cases, each with a unique way of iterating over the arr
array and applying the someFn
function to each element. The test cases are:
forEach
loopfor
loop with an index variablefor
loop without an index variablemap()
methodfor...of
loopLibrary and Special JS Features
None of the test cases use any external libraries or special JavaScript features beyond what is standard in modern browsers.
Approach Comparison
The five approaches are compared to determine which one performs best:
forEach
method, which iterates over the array using a hidden iterator object.for
loop with an index variable i
, where i
is incremented on each iteration.for
loop without an index variable, relying on the arr.length
property to determine the number of iterations.map()
method, which applies a transformation function to each element of the array and returns a new array with the transformed values.for...of
loop, which iterates over the array using an iterator object that yields the current value on each iteration.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Other Considerations
When choosing an iteration approach, consider factors such as:
It's also worth noting that the JavaScript engine may optimize certain approaches more than others. For example, some engines may optimize the forEach
method by reusing existing iterator objects.
Alternatives
If you're interested in exploring alternative iteration methods or testing other JavaScript features, here are a few suggestions:
You can also experiment with other JavaScript features, such as:
Remember to consider factors such as readability, maintainability, performance, and memory usage when testing new approaches or features.