Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..of, but with rich object array, but fair
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for..of
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [...Array(100000)].map(() => ({ label: Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10), value: Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10) }))
Tests:
for
for (var i = 0, n = array.length; i < n; i++) { array[i]; }
foreach
array.forEach(function(i) { i; });
for..of
for (var i of array) { i; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for
foreach
for..of
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):
**What is being tested?** On MeasureThat.net, the provided benchmark tests the performance of three different loop constructs in JavaScript: 1. `for` loops 2. `.forEach()` method (also known as Array.prototype.forEach()) 3. `.for...of` loops (introduced in ECMAScript 2015) These loops are used to iterate over an array and execute some code for each element. **Options being compared:** The benchmark compares the performance of these three loop constructs when iterating over a large array with 100,000 elements. The array is populated with random objects, but their properties are not accessed or modified during the iteration, as specified in the "fair" parameter of the Benchmark Definition. **Pros and Cons of each approach:** 1. **`for` loops**: * Pros: More control over iteration variables and loop behavior. * Cons: Can be slower due to explicit variable management and looping logic. 2. `.forEach()` method**: * Pros: Simplifies iteration code and reduces boilerplate, making it easier to read and maintain. * Cons: May incur additional overhead due to method call and function execution. 3. `.for...of` loops**: * Pros: Provides a concise and expressive way to iterate over arrays, with fewer explicit variables to manage. * Cons: Relatively new feature in ECMAScript, which may not be supported by older browsers or engines. **Other considerations:** * The benchmark uses `Math.random()` to generate random array elements, which can affect performance due to the hash-based lookup of strings. * The use of `.map()` to populate the array with 100,000 elements adds additional overhead due to function invocation and iteration. * The "fair" parameter in the Benchmark Definition aims to eliminate the impact of other JavaScript features or optimizations that might skew the results. **Library usage:** There is no explicit library used in this benchmark. However, some libraries (e.g., `lodash`) may provide optimized implementation of Array.prototype.forEach() that could affect performance. **Special JS feature/syntax:** The `.for...of` loop uses a relatively new syntax introduced in ECMAScript 2015. This syntax allows for concise iteration over arrays without the need for explicit variable management. **Alternatives:** If you're interested in exploring alternative loop constructs or optimization techniques, consider: * Using `let` and `const` instead of `var` to declare loop variables. * Utilizing `for...in` loops with prototype chains for iterating over objects. * Implementing manual iteration using indices and pointer arithmetic (e.g., C-style indexing). * Leveraging Just-In-Time (JIT) compilation or native code generation for performance-critical loops. Keep in mind that each approach has its trade-offs, and the best choice depends on your specific use case, performance requirements, and desired coding style.
Related benchmarks:
Regular for vs forEach
map vs forEach Chris
map vs forEach Chris v2
map vs forEach Chris v2b
Array.forEach vs Object.keys().forEach
Comments
Confirm delete:
Do you really want to delete benchmark?