Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Iteration through array; of vs forEach
(version: 1)
Comparing performance of:
of vs forEach
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
arr = [1,2,3,4,5]
Tests:
of
for (val of arr) Number(val)
forEach
arr.forEach(val => Number(val))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
of
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.3 Safari/605.1.15
Browser/OS:
Safari 26 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
of
43173324.0 Ops/sec
forEach
133998216.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmarking test cases. **What is being tested?** The test case measures the performance difference between two approaches to iterate over an array and perform an operation on each element: `forEach` vs direct iteration using `for...of`. **Options compared:** 1. **`forEach"`**: This is a built-in JavaScript method that allows iterating over an iterable object (like an array) while executing a block of code for each element. 2. **Direct iteration using `for...of```**: This approach uses the `for...of` loop syntax to iterate over an array, which is a more traditional and explicit way of looping. **Pros and Cons:** * **`forEach"`:** * Pros: * More concise and readable code. * Less error-prone than direct iteration using `for...of`. * Cons: * May have performance implications due to the overhead of calling a method on each array element. * **Direct iteration using `for...of```**: * Pros: * More control over loop behavior and potential for better optimization by the JavaScript engine. * Less reliance on built-in methods, which can lead to more predictable performance. * Cons: * May be less readable or more error-prone than using `forEach`. * **Other considerations:** * The choice between these two approaches may also depend on other factors like code maintainability, readability, and personal preference. **Library usage:** There are no libraries used in this benchmarking test case. It only involves standard JavaScript features. **Special JavaScript feature or syntax:** The `for...of` loop is a special syntax that allows iterating over arrays and other iterable objects. The `.forEach()` method is also a built-in JavaScript method for iterating over an array. **Benchmark preparation code:** The script preparation code `arr = [1,2,3,4,5]` initializes an array with five elements for the benchmark.
Related benchmarks:
foreach vs for..of
for (i < n) vs forEach vs for...of
foreach vs for...of
Array fill vs for i loop
Comments
Confirm delete:
Do you really want to delete benchmark?