Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For, forEach, for of, Map
(version: 0)
Comparing performance of:
For vs forEach vs For of vs map
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (let i = 0; i < 100000; i++) { arr[i] = i; } function someFn(i) { return i * 3 * 8; }
Tests:
For
for (var x = 0; x < arr.length; x++) { someFn(arr[x]); }
forEach
arr.forEach(function (item){ someFn(item); })
For of
for(let item of arr) { someFn(item) }
map
arr.map((item) => someFn(item))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
For
forEach
For of
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark JSON and explain what's being tested, along with the pros and cons of each approach. **Benchmark Definition** The benchmark defines a common script preparation code for all test cases: ```javascript var arr = []; for (let i = 0; i < 100000; i++) { arr[i] = i; } function someFn(i) { return i * 3 * 8; } ``` This script creates an array `arr` of length 100,000 and defines a simple function `someFn` that takes an integer `i` as input. **Individual Test Cases** There are four test cases: 1. **For**: This test case uses the traditional `for` loop to iterate over the `arr` array and call the `someFn` function for each element. ```javascript for (let x = 0; x < arr.length; x++) { someFn(arr[x]); } ``` Pros: Simple and straightforward implementation. Cons: This approach can be slower due to the overhead of the loop variable `x` and the increment operation inside the loop. 2. **ForEach**: This test case uses the `forEach` method to iterate over the `arr` array and call the `someFn` function for each element. ```javascript arr.forEach(function (item) { someFn(item); }); ``` Pros: More concise and expressive than the traditional `for` loop. Cons: The `forEach` method can be slower due to the overhead of the callback function and the iteration logic. 3. **For of**: This test case uses the `for...of` loop to iterate over the `arr` array and call the `someFn` function for each element. ```javascript for (let item of arr) { someFn(item); } ``` Pros: More concise and expressive than the traditional `for` loop, with better support for iterable objects. Cons: This approach can be slower due to the overhead of the loop variable `item` and the iteration logic. 4. **Map**: This test case uses the `map()` method to create a new array that applies the `someFn` function to each element of the original `arr` array. ```javascript arr.map((item) => someFn(item)); ``` Pros: More concise and expressive than the traditional `for` loop, with better support for functional programming. Cons: This approach can be slower due to the overhead of creating a new array and applying the function to each element. **Library Usage** None of the test cases use any external libraries. **Special JS Features/Syntax** The test cases do not explicitly use any special JavaScript features or syntax, such as async/await, promises, or modern ES6+ features. **Other Alternatives** There are other ways to benchmark similar operations, such as: * Using `Array.prototype.every()` instead of `forEach` * Using a `while` loop instead of the traditional `for` loop * Using a different data structure, such as an array with object values instead of primitive values * Using a different function or algorithm for computing the result However, these alternatives may not be representative of real-world use cases and may introduce bias in the benchmark results.
Related benchmarks:
Array loop: forEach vs for vs map vs for of entries
Array loop vs foreach vs map -2
Array loop vs foreach vs map e
Array loop vs foreach vs map with large array
Array map vs forEach
Comments
Confirm delete:
Do you really want to delete benchmark?