Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
While vs For loop
(version: 0)
Comparing performance of:
For loop vs While loop
Created:
9 years ago
by:
Guest
Jump to the latest result
Tests:
For loop
var acts = "BRA|BRA|FLR|VSL|BRA".split("|"); for (var i = 0; i < acts.length -1; i++) { if (acts[i] == "BRA") acts.splice(i, 1); }
While loop
var acts = "BRA|BRA|FLR|VSL|BRA".split("|"); var i = acts.length; while (i--) { if (acts[i] == "BRA") acts.splice(i, 1); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
For loop
While loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
14 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:149.0) Gecko/20100101 Firefox/149.0
Browser/OS:
Firefox 149 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For loop
2795576.2 Ops/sec
While loop
2062276.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the benchmark in detail. **Benchmark Overview** The benchmark compares the performance of two loops: For Loop and While Loop, specifically when removing elements from an array using `splice()` method. The goal is to determine which loop approach is faster on Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0. **Options Compared** The benchmark compares two approaches: 1. **For Loop**: A traditional for loop uses an incrementing index to iterate over the array elements. ```javascript for (var i = 0; i < acts.length -1; i++) { if (acts[i] == "BRA") acts.splice(i, 1); } ``` 2. **While Loop**: A while loop continues to execute as long as a condition is true. In this case, the loop iterates over the array elements using a decrementing index. ```javascript var i = acts.length; while (i--) { if (acts[i] == "BRA") acts.splice(i, 1); } ``` **Pros and Cons** * **For Loop**: + Pros: Can be more intuitive for developers familiar with traditional loops. + Cons: May lead to off-by-one errors or indexing issues due to the incrementing index. * **While Loop**: + Pros: Allows for more flexibility in controlling the loop iteration, such as decrementing the index. + Cons: Can be less intuitive for developers not familiar with while loops. **Library and Special JS Features** The benchmark uses no specific libraries or special JavaScript features. However, it's worth noting that `splice()` method modifies the original array, which can lead to unexpected behavior in certain scenarios. **Other Alternatives** If you want to explore alternative loop approaches, consider: 1. **Iterators**: Use iterators (e.g., `for...of`) instead of traditional loops. 2. **Array.prototype.forEach()**: Utilize the built-in `forEach()` method for a more modern and efficient way to iterate over arrays. In conclusion, this benchmark provides a simple yet informative comparison between two common loop approaches in JavaScript: For Loop and While Loop. By understanding the trade-offs and pros and cons of each approach, developers can make informed decisions when writing their own code.
Related benchmarks:
index loop vs for-of loop vs foreach vs map
Array loop: forEach vs for vs map vs for of entries
For vs Foreach vs Do While vs While
For vs Foreach vs Do While vs While v3
For vs Foreach vs While
Comments
Confirm delete:
Do you really want to delete benchmark?