Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs for of vs for in
(version: 0)
Comparing performance of:
for vs for of vs for in
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
for
const arr = Array(10000).fill(0); const l = arr.length; for (let i = 0; i < l; i++) { arr[i]; }
for of
const arr = Array(10000).fill(0); for (let x of arr) { x; }
for in
const arr = Array(10000).fill(0); for (let i in arr) { arr[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for
for of
for in
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
16572.1 Ops/sec
for of
8303.3 Ops/sec
for in
454.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, comparing different approaches for various use cases. **Benchmark Definition** The provided JSON represents the benchmark definition with three test cases: * "for" * "for of" * "for in" The script preparation code for each test case is not specified in the JSON, but it's likely that a loop is created to iterate over an array. The HTML preparation code is also empty. **Options Compared** Three options are compared: 1. **For Loop** 2. **For...Of Loop** 3. **For...In Loop** Each of these loops has its own pros and cons: * **For Loop**: This loop uses a traditional approach with an incrementing index variable (e.g., `i`). It's generally considered the most readable and maintainable option, but it can be slower due to the overhead of indexing. * **For...Of Loop**: This loop uses an iterator protocol (similar to Python's `for` loop) with an iterable object (e.g., `arr`). It's concise and often faster than traditional loops because it avoids indexing. However, it may not be suitable for all scenarios or legacy codebases. * **For...In Loop**: This loop uses a loop that iterates over the properties of an object (e.g., `arr` as if it were an object). It can be slower and less readable than traditional loops due to the overhead of property lookup. However, it's useful when working with objects or arrays with non-standard iteration methods. **Library** There is no library explicitly mentioned in the provided JSON, but JavaScript's built-in `Array` and `String` prototypes use various internal methods for iteration, such as `length`, `indexOf`, and `forEach`. **Special JS Features** The test cases do not explicitly mention any special JavaScript features or syntax, such as: * Async/await * Promises * Generators * Closures However, it's worth noting that the `For...Of` loop uses a feature introduced in ECMAScript 2015 (ES6). **Alternative Approaches** If you're looking for alternative approaches to these loops, consider the following: * **Map(), Filter(), Reduce()**: These methods can be used to process arrays without explicit looping. They're often faster and more concise than traditional loops. * **Iterators**: Custom iterators can be created using the `Iterator` interface and `next()` method to iterate over objects or arrays. * **Recursion**: Recursive functions can be used to solve problems iteratively, but they may lead to stack overflow errors for large inputs. Keep in mind that performance optimizations should always consider the specific use case and requirements of your application.
Related benchmarks:
For in vs For of
foreach vs for vs for in
foreach vs for...of
for of vs forEach with console log
forEach vs for of 7
Comments
Confirm delete:
Do you really want to delete benchmark?