var arr = [];
for (var i = 0; i < 1000; i++) {
arr[i] = i;
}
function someFn(i) {
return i * 3 * 8;
}
arr.forEach(function (item){
someFn(1);
})
for (var i = 0, len = arr.length; i < len; i++) {
someFn(1);
}
arr.map(item => someFn(1))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
foreach | |
for | |
map |
Test name | Executions per second |
---|---|
foreach | 10577.0 Ops/sec |
for | 11585.7 Ops/sec |
map | 10506.5 Ops/sec |
Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, comparing the performance of different approaches for various tasks.
Benchmark Definition JSON
The provided benchmark definition JSON contains three test cases:
Tested Approaches
The benchmark compares three approaches for iterating over an array:
for
loop that accesses each element individually.forEach
method, which calls a callback function for each element in the array.map
method, which returns a new array with the results of applying a given function to each element.Pros and Cons
Library Usage
None of the provided test cases use any external libraries. However, it's worth noting that someFn
is a function defined within the script preparation code, which could be considered part of the benchmark itself.
Special JS Features/Syntax
There are no special JavaScript features or syntax used in these test cases. The focus is on comparing three common approaches for iterating over an array.
Other Alternatives
For similar benchmarks, you might want to explore other approaches, such as:
lodash
for functional programming.Keep in mind that the choice of alternative depends on your specific use case and goals.