Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.forEach vs for const of
(version: 0)
Comparing performance of:
.forEach vs for const of
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = [...Array(10000).keys()]; var sum = 0;
Tests:
.forEach
a.forEach(i => sum += i)
for const of
for (const i of a) { sum += i; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
.forEach
for const of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.forEach
2060.5 Ops/sec
for const of
2045.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the provided benchmark and explain what's being tested. **Benchmark Definition** The provided JSON defines two benchmark tests: `.forEach` and `for const of`. These are both used to iterate over an array, but they use different approaches to achieve this. **Options Compared** The two options being compared are: 1. `.forEach`: This method iterates over the elements of an array using a callback function. The callback function is called for each element in the array, and it receives the current element as an argument. 2. `for const of`: This is a newer syntax introduced in ECMAScript 2017 (ES7). It uses a `for...of` loop to iterate over an iterable object (such as an array) without exposing its internal implementation details. **Pros and Cons** Both approaches have their advantages and disadvantages: * `.forEach`: + Pros: Widely supported, easy to use, and compatible with older browsers. + Cons: Can be less efficient than `for const of`, as it creates a new scope for each iteration, which can lead to increased memory usage. * `for const of`: + Pros: More efficient, as it avoids the overhead of creating a new scope for each iteration. It also provides better performance, as it uses a more optimized internal loop implementation. + Cons: Requires modern JavaScript engines (like Chrome 119) to execute, and might not be compatible with older browsers or environments. **Library Usage** There is no library used in this benchmark. **Special JS Features/Syntax** The `for const of` syntax uses the `for...of` loop, which was introduced in ECMAScript 2017 (ES7). This syntax allows you to iterate over an array without exposing its internal implementation details. It's a more modern and efficient way of iterating over arrays compared to the traditional `.forEach` method. **Other Alternatives** If you're interested in exploring other alternatives for iterating over arrays, here are a few: 1. `for...in`: This loop iterates over an object's properties using their keys. 2. `map()`, `filter()`, and `reduce()`: These methods are used to transform or filter arrays without iterating over them directly. In conclusion, the provided benchmark compares two approaches for iterating over arrays: `.forEach` and `for const of`. The `for const of` syntax is a more modern and efficient way of iterating over arrays, but it requires modern JavaScript engines to execute.
Related benchmarks:
for..of vs foreach
foreach vs for vs for in
for of vs Array.reduce vs Array.forEach vs for i for summing and array of integers
for of vs forEach with add operation
Comments
Confirm delete:
Do you really want to delete benchmark?