Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For vs for of redux
(version: 0)
Comparing performance of:
for vs for of
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array.from(Array(1000), () => Math.random())
Tests:
for
const len = arr.length; let ret = 0; for (let i = 0; i < len; i++) ret += arr[i];
for of
let ret = 0; for (const v of arr) ret += v;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for
for of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
35684.7 Ops/sec
for of
768320.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what's being tested, compared, and some of the pros and cons associated with each approach. **Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark tests two approaches for iterating over an array: traditional `for` loops and the `for...of` loop syntax. **Script Preparation Code** The script preparation code initializes an array of 1000 random numbers using `Array.from()` method, which is a modern JavaScript API that allows creating arrays from iterable objects or array-like objects. The `Math.random()` function generates random numbers between 0 and 1. **Html Preparation Code** There is no HTML preparation code provided, so we can assume it's not being used in this benchmark. **Individual Test Cases** The two test cases are: ### For Loop ```javascript const len = arr.length; let ret = 0; for (let i = 0; i < len; i++) { ret += arr[i]; } ``` This is a traditional `for` loop that uses an index variable (`i`) to iterate over the array and accumulates the sum in the `ret` variable. ### For...of Loop ```javascript let ret = 0; for (const v of arr) { ret += v; } ``` This uses the `for...of` loop syntax, which is a more modern way to iterate over arrays. It iterates directly over the array values and accumulates the sum in the `ret` variable. **Pros and Cons** Here's a brief summary of the pros and cons associated with each approach: * **Traditional For Loop** + Pros: - Wide browser support (long-standing JavaScript syntax) - Easy to understand for beginners + Cons: - Less readable due to the index variable (`i`) - May lead to off-by-one errors if not handled carefully * **For...of Loop** + Pros: - More concise and expressive code - Direct iteration over array values eliminates indexing issues - Modern JavaScript syntax (widely adopted in recent browsers) + Cons: - Requires support for `for...of` loop syntax, which may not be universally supported **Library and Special JS Features** In this benchmark, no libraries are used. The `Array.from()` method is a built-in JavaScript API. No special JavaScript features or syntaxes are required to run these test cases. **Other Alternatives** If you're interested in exploring alternative iteration methods for arrays, here are a few: 1. **`forEach()` method**: Similar to `for...of`, but returns undefined when the array is empty. 2. **`map()`, `reduce()`, and other Array.prototype methods**: These can be used to process arrays, but may not provide direct iteration like `for` or `for...of`. 3. **Arrow functions with spread operators**: These offer a concise way to iterate over objects, but are not directly applicable to arrays. Keep in mind that each approach has its strengths and weaknesses, and the choice of iteration method depends on your specific use case, personal preference, and target browser support.
Related benchmarks:
Array .push() vs .unshift() with random numbers
repeated Math.random() vs crypto.getRandomValues()
Math.random vs Crypto.getRandomValues for 10k values
new Array() vs Array.from() with random data
Array.from VS spreading for
Comments
Confirm delete:
Do you really want to delete benchmark?