Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For vs Foreach vs Do While vs While v3
(version: 0)
See who is faster
Comparing performance of:
For vs Foreach vs Do While vs While
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100);
Tests:
For
let n = array.length; for (let i = 0; i < n; ++i) { array[i]; }
Foreach
array.forEach(function(i) { array[i]; });
Do While
var i = 0; do { array[i]; i++; } while (i < 100);
While
var i = 0; while (i < 100) { array[i]; i++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
For
Foreach
Do While
While
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:132.0) Gecko/20100101 Firefox/132.0
Browser/OS:
Firefox 132 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For
27379612.0 Ops/sec
Foreach
13874883.0 Ops/sec
Do While
17427592.0 Ops/sec
While
27500722.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark compares the performance of four different looping constructs: `for`, `foreach`, `do-while`, and `while` (version 3). Each test case measures how many executions per second are possible. **Test Cases** The benchmark consists of four individual test cases: 1. **For**: This loop iterates over an array using a traditional `for` loop, incrementing the index `i`. 2. **Foreach**: This loop uses the `forEach` method to iterate over the array, with each iteration accessing the element at index `i`. 3. **Do-While**: This loop starts from index 0 and iterates as long as `i` is less than 100. 4. **While (v3)**: This loop also starts from index 0 and iterates as long as `i` is less than 100, but uses a different syntax. **Options Compared** The benchmark compares the performance of these four looping constructs: * **For**: Traditional `for` loop with manual index increment. * **Foreach**: `forEach` method to iterate over an array. * **Do-While**: Loop that starts from index 0 and continues as long as a condition is true. * **While (v3)**: Similar to the traditional `while` loop, but uses version 3 syntax. **Pros and Cons of Each Approach** Here's a brief overview of the pros and cons of each approach: * **For**: Pros: Simple, efficient. Cons: Can be error-prone due to manual index management. * **Foreach**: Pros: Concise, easy to read. Cons: May incur overhead due to the `forEach` method. * **Do-While**: Pros: More flexible than traditional `while` loops, as it can start from any index. Cons: Can be less efficient due to the need to manually increment the index. * **While (v3)**: Same pros and cons as traditional `while` loops. **Libraries and Special Features** The benchmark uses the following libraries: * None explicitly mentioned in the provided JSON data, but it's likely that MeasureThat.net itself is a library of some sort. No special JavaScript features or syntax are used in this benchmark. All examples are standard, compliant JavaScript code. **Other Alternatives** For those interested in exploring alternative looping constructs or optimizations, here are some options: * **Iterators**: Instead of using `forEach`, consider creating an iterator function to iterate over the array. * **Array.prototype.every` and `Array.prototype.some`: For loops that use these methods can be more efficient than traditional `for` loops. * **Reducing arrays**: If you need to perform multiple operations on each element, consider reducing the array instead of looping over it. Keep in mind that optimizations may depend on specific requirements and use cases. MeasureThat.net's benchmark provides a good starting point for exploring performance differences between different looping constructs, but be sure to test your own scenarios for optimal results.
Related benchmarks:
Array fill foreach, vs for i loop
For vs Foreach vs Do While vs While
Array fill map, vs for i loop
Array fill vs for i loop
Comments
Confirm delete:
Do you really want to delete benchmark?