Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array loop vs foreach vs map vs for w/o fn call - with console.log
(version: 0)
Comparing performance of:
foreach vs for vs map vs for no fn call
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 1000; i++) { arr[i] = i; } function someFn(i) { return i * 3 * 8; }
Tests:
foreach
arr.forEach(function (item){ console.log(someFn(item)); })
for
for (var i = 0, len = arr.length; i < len; i++) { console.log(someFn(arr[i])); }
map
arr.map(item => console.log(someFn(item)))
for no fn call
for (var i = 0, len = arr.length; i < len; i++) { console.log(i * 3 * 8); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
foreach
for
map
for no fn call
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 definitions and explain what's being tested, along with the pros and cons of each approach. **Benchmark Definition Overview** The main goal of this benchmark is to compare the performance of four different ways to iterate over an array in JavaScript: 1. `forEach` 2. Traditional `for` loop 3. `map` 4. Traditional `for` loop without a function call (i.e., just printing the result directly) **Array Preparation** Before running the benchmarks, the script prepares an array of 1000 elements with values ranging from 0 to 999. **Library: None** There are no external libraries used in this benchmark. **Special JavaScript Features or Syntax** None mentioned. **Benchmark Test Cases** Here's a breakdown of each test case: 1. `foreach`: ```javascript arr.forEach(function (item) { console.log(someFn(item)); }); ``` This uses the `forEach` method to iterate over the array, calling `someFn` for each element. Pros: Easy to read and maintain; concise. Cons: May have performance overhead due to function call overhead. 2. Traditional `for` loop: ```javascript for (var i = 0, len = arr.length; i < len; i++) { console.log(someFn(arr[i])); } ``` This uses a traditional `for` loop to iterate over the array, calling `someFn` for each element. Pros: Fast and efficient; can be optimized with caching. Cons: More verbose than `forEach`. 3. `map`: ```javascript arr.map(item => console.log(someFn(item))) ``` This uses the `map` method to create a new array with the results of applying `someFn` to each element. Pros: Concise and expressive; can be optimized with caching. Cons: May have performance overhead due to function call overhead. 4. Traditional `for` loop without a function call: ```javascript for (var i = 0, len = arr.length; i < len; i++) { console.log(i * 3 * 8); } ``` This uses a traditional `for` loop to iterate over the array, printing the result directly. Pros: Fast and efficient. Cons: More verbose than `forEach`. **Benchmark Results** The latest benchmark results show that: * `map` is the fastest (30.975736618041992 executions per second) * `foreach` is slower but still relatively fast (30.572250366210938 executions per second) * Traditional `for` loop without a function call is faster than traditional `for` loop with a function call (22.446043014526367 vs 20.981002807617188 executions per second) **Other Alternatives** If you wanted to test other approaches, here are some additional options: * `reduce` * Using `Array.prototype.every()` or `Array.prototype.some()` * Using a custom iterator function * Using Web Workers or parallel processing However, these alternatives may not be relevant for this specific benchmark, which is focused on comparing the performance of iterating over an array in JavaScript.
Related benchmarks:
Array loop vs foreach vs map (Small arrays)
Array loop: forEach vs for vs map vs for of entries
Array loop vs foreach vs map -2
Array loop vs foreach vs map with large array
Array loop vs for of loop vs foreach vs map fixed
Comments
Confirm delete:
Do you really want to delete benchmark?