Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..of, but with rich object array
(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) { array[i]; });
for..of
for (var i of array) { 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):
Let's break down the provided benchmark and explain what's being tested, compared, and discussed. **Benchmark Overview** The website MeasureThat.net provides a platform for creating and running JavaScript microbenchmarks. The provided JSON represents a benchmark with three test cases: `for`, `foreach`, and `for..of`. These loop constructs are commonly used in JavaScript programming, and the benchmark compares their performance when iterating over an array of 100,000 objects. **Test Case Descriptions** Each test case is defined by a separate object with two properties: 1. **Benchmark Definition**: This property contains the JavaScript code that defines the loop construct being tested. There are three variants: * `for` loop: Iterates using traditional increment-decrement syntax. * `foreach` loop: Uses the `forEach()` method to iterate over the array elements. * `for..of` loop: Uses the `for...of` loop syntax to iterate over the array elements. 2. **Test Name**: This property provides a human-readable name for each test case. **Loop Construct Comparison** The three loop constructs are compared in terms of performance, measured by the number of executions per second (ExecutionsPerSecond). The benchmark measures the execution speed of each loop construct when iterating over the array defined in the `Script Preparation Code`. Here's a brief summary of the pros and cons of each loop construct: 1. **For Loop**: * Pros: Wide support across browsers, easy to read and maintain. * Cons: Can be slower than other constructs due to increment-decrement overhead. 2. **Foreach Loop**: * Pros: Simple and concise syntax, good performance in modern browsers. * Cons: May not work well with older browsers or specific edge cases. 3. **For...Of Loop**: * Pros: Modern syntax is more expressive and readable, good performance in modern browsers. * Cons: Limited support across older browsers, may require additional setup. **Library Usage** None of the test cases explicitly use any libraries. However, the `Array` class is used to create an array of 100,000 objects, which is a built-in JavaScript object. **Special JS Features or Syntax** The benchmark uses modern JavaScript features: 1. **Arrow functions**: Used in the `foreach` loop definition. 2. **Template literals**: Used in the `Script Preparation Code` to generate random string values for array elements. 3. **For...of loop syntax**: Used in the `for..of` loop definition. **Other Alternatives** There are alternative loop constructs available in JavaScript, such as: 1. **While loops**: Can be used instead of for loops for more control over iteration. 2. **Recursive functions**: Can be used to iterate over arrays, but may lead to performance issues and stack overflow errors. However, the `for`, `foreach`, and `for...of` loops are the most commonly used and widely supported loop constructs in JavaScript.
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?