itemList = [0,1,2,3,4,5,6,7,8,9];
for(let index= 0; index<itemList.legth; index++){
const count = itemList[index];
};
itemList = [0,1,2,3,4,5,6,7,8,9];
itemList.forEach(item => {
const count = item;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
For | |
Foreach |
Test name | Executions per second |
---|---|
For | 15458005.0 Ops/sec |
Foreach | 10651310.0 Ops/sec |
Overview of the Benchmark
The provided benchmark measures the performance difference between using traditional for
loops and the forEach
method in JavaScript for iterating over arrays.
Benchmark Definition
The benchmark definition consists of two test cases:
: This test case uses a traditional
forloop to iterate over an array, where each iteration assigns the value at the current index to a variable called
count`.: This test case uses the
forEachmethod to iterate over the same array, where each iteration also assigns the value of each item to a variable called
count`.Options Compared
The benchmark compares two options:
for
loopforEach
methodPros and Cons of Each Approach
for
loop:forEach
does.forEach
method:Library Usage
None of the test cases explicitly use any libraries. However, it's worth noting that the forEach
method is a built-in part of the JavaScript standard library since ECMAScript 5 (ES5).
Special JS Feature/Syntax
There are no special features or syntaxes used in this benchmark.
Other Alternatives
If you want to compare other iteration methods, such as:
You would need to add additional test cases to measure their performance.
I hope this explanation helps!