Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test 100k obj check
(version: 1)
Comparing performance of:
foreach vs for vs map vs for of
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 100000; i++) { arr[i] = i; } function someFn(i) { return i === 42; }
Tests:
foreach
arr.forEach(function (item){ someFn(item); })
for
for (var i = 0, len = arr.length; i < len; i++) { someFn(arr[i]); }
map
arr.map(item => someFn(item))
for of
for (var item of arr) { someFn(item); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
foreach
for
map
for of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
foreach
2176.1 Ops/sec
for
4967.8 Ops/sec
map
810.8 Ops/sec
for of
4182.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark definition and test cases measure the performance of different approaches for iterating over an array in JavaScript. The benchmark consists of two parts: a script preparation code and HTML preparation code. The script preparation code initializes an array with 100,000 elements and defines a function `someFn` that takes an integer as input and returns `true` if it's equal to 42. The HTML preparation code is empty. **Options Compared** The benchmark compares four different approaches for iterating over the array: 1. **forEach**: Iterates over the array using the `forEach` method. 2. **for**: Iterates over the array using a traditional `for` loop. 3. **map**: Iterates over the array and applies the `someFn` function to each element using the `map` method. 4. **for of**: Iterates over the array using the `for...of` loop. **Pros and Cons of Each Approach** 1. **forEach**: * Pros: Concise and easy to read, doesn't require a loop counter. * Cons: Can be slower than traditional loops due to overhead from the `forEach` method. 2. **for**: * Pros: Fastest iteration approach, allows for more control over the loop. * Cons: Requires a loop counter, can be less readable. 3. **map**: * Pros: Can be used in conjunction with other methods, provides a concise way to transform data. * Cons: Iterates over the array twice (once for `map` and once for `someFn`), can be slower due to this extra iteration. 4. **for of**: * Pros: Concise and easy to read, doesn't require a loop counter. * Cons: Less readable than traditional loops, can be slower due to the overhead from the `for...of` loop. **Library Used** There is no library explicitly mentioned in the benchmark definition. However, the use of modern JavaScript features such as arrow functions (`item => someFn(item)`) and template literals (`arr.forEach(function (item){\r\n someFn(item);\r\n})`) suggests that the benchmark uses ECMAScript 6+ syntax. **Special JS Feature or Syntax** The benchmark uses several modern JavaScript features, including: * Arrow functions (`item => someFn(item)`): Introduced in ECMAScript 2015. * Template literals (`\r\n` escapes and backticks): Introduced in ECMAScript 2015. * `for...of` loop: Introduced in ECMAScript 2015. **Alternatives** Other alternatives for iterating over an array in JavaScript include: * Traditional `while` loops * `Array.prototype.reduce()` method (although this method requires two arguments, a callback function and an initial value) * `Array.prototype.every()` or `Array.prototype.some()` methods (although these methods are typically used for filtering or checking if all/any elements pass a test) Note that the performance of different iteration approaches can vary depending on the specific use case and the JavaScript engine being used. MeasureThat.net provides a useful platform for comparing the performance of different approaches in a controlled environment.
Related benchmarks:
Lodash.js vs Nativeыы
Lodash.js vs Native isArrary
Lodash.js vs Native MAGIC
Lodash.js vs Native Map 11111111232312
Lodash.js vs Native1
Comments
Confirm delete:
Do you really want to delete benchmark?