Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
loops: for vs foreach vs some vs for..of vs for..in
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs some vs for..of vs for..in
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(1000);
Tests:
for
for (var i = 0; i < array.length; i++) { array[i]; }
foreach
array.forEach(function(i) { array[i]; });
some
array.some(function(i) { array[i]; });
for..of
for (var i of array) { array[i]; }
for..in
for (var i in array) { array[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for
foreach
some
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
219582.8 Ops/sec
foreach
334843.8 Ops/sec
some
315152.7 Ops/sec
for..of
46981.5 Ops/sec
for..in
472841.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **What is tested?** MeasureThat.net is testing the performance of four different loop types in JavaScript: 1. `for` loop 2. `forEach` method 3. `some` method 4. `for...of` loop (introduced in ECMAScript 2015) 5. `for...in` loop (introduced in ECMAScript 1999) The test cases are designed to measure the performance of each loop type on a large array of 1000 elements. **Options compared** Each test case is comparing the performance of two or more loop types, resulting in multiple runs for each test. The options being compared include: * `for` loop vs. `forEach` method * `forEach` method vs. `some` method * `some` method vs. `for...of` loop * `for` loop vs. `for...of` loop * `for` loop vs. `for...in` loop **Pros and cons of each approach** Here's a brief summary of the pros and cons of each loop type: 1. **`for` loop**: * Pros: Control over iteration, easy to implement, works well for small arrays. * Cons: Can be slower than other methods, may require more memory allocation. 2. **`forEach` method**: * Pros: Concise syntax, works well with array methods, efficient execution. * Cons: May not work as expected if array is modified during iteration. 3. **`some` method**: * Pros: Efficient and concise syntax, suitable for testing conditional logic. * Cons: May be slower than `for` loop or `forEach` method due to overhead of function invocation. 4. **`for...of` loop** (ECMAScript 2015): * Pros: More expressive syntax, efficient execution, works well with arrays and iterables. * Cons: Less control over iteration compared to traditional `for` loops. 5. **`for...in` loop** (ECMAScript 1999): * Pros: Works well with object properties, efficient execution. * Cons: May not be suitable for array-based operations due to property iteration. **Library usage** None of the test cases explicitly use any external libraries. However, `forEach` and `some` methods are built-in methods in JavaScript, which are part of the language standard. **Special JS features or syntax** There are no special JS features or syntax used in these test cases. **Other alternatives** Other loop types that could be tested include: * `while` loops * Array destructuring (e.g., using destructing assignment) * Other array methods, such as `map`, `filter`, or `reduce` However, these additional tests would require modifications to the benchmark setup and test cases. **Benchmark preparation code** The script preparation code creates an array of 1000 elements, which is a common use case for testing loop performance. The HTML preparation code is empty, indicating that no additional DOM-related work is performed before running the benchmarks.
Related benchmarks:
foreach vs for..of
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
for (cache length) vs foreach vs for..in vs for..of
for vs foreach vs some vs for..of big (over a million runs)
Comments
Confirm delete:
Do you really want to delete benchmark?