Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
While vs For loop vs Map
(version: 0)
Comparing performance of:
For loop vs While loop vs Map loop
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var acts = "BRA|BRA|FLR|VSL|BRA".split("|");
Tests:
For loop
for (var i = 0; i < acts.length -1; i++) { if (acts[i] == "BRA") acts.splice(i, 1); }
While loop
var i = acts.length; while (i--) { if (acts[i] == "BRA") acts.splice(i, 1); }
Map loop
acts.map((act,i)=>{ if (act == "BRA") acts.splice(i, 1); })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
For loop
While loop
Map loop
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_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For loop
4804802.5 Ops/sec
While loop
9228352.0 Ops/sec
Map loop
20004164.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different JavaScript loops can be an interesting and instructive exercise. **Benchmark Overview** The provided benchmark is designed to measure the performance of three different types of loops: For loop, While loop, and Map loop. The script generates an array `acts` with a specific pattern (BRA|BRA|FLR|VSL|BRA) and then uses each loop type to remove the first occurrence of "BRA" from the array. **Options Compared** The benchmark compares three different approaches: 1. **For loop**: Uses a traditional for loop to iterate over the indices of the `acts` array. 2. **While loop**: Uses a while loop to decrement the index variable until it reaches the end of the array. 3. **Map loop**: Uses the Array.prototype.map() method to create a new array with the modified elements, effectively removing the first occurrence of "BRA" from each element. **Pros and Cons** * **For loop**: Easy to understand and implement, but can be slower due to the overhead of index calculations. * **While loop**: Can be faster than for loops because it avoids the overhead of incrementing an index variable. However, it requires manual management of the loop condition. * **Map loop**: Often the fastest option, as it uses a more efficient algorithm and avoids the need for manual iteration. **Library Usage** The benchmark uses the Array.prototype.map() method, which is a built-in JavaScript function that applies a given function to each element in an array. The purpose of this library is to provide a concise and expressive way to create new arrays with modified elements. **Special JS Features/Syntax** None mentioned explicitly, but it's worth noting that the use of the spread operator (`...`) in the script preparation code is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). However, it's unlikely to have a significant impact on performance compared to other factors like loop optimization and browser differences. **Other Alternatives** If you want to explore alternative approaches, here are some options: * **Reduce**: Instead of using map() or a for/while loop, you can use the Array.prototype.reduce() method to achieve the same result. * **Filter**: You can also use the Array.prototype.filter() method to create a new array with filtered elements. * **Native functions**: Depending on the browser and JavaScript version, native functions like `Array.prototype.forEach()` or `Array.prototype.every()` might be suitable alternatives. For a more detailed analysis of the performance differences between these approaches, I recommend exploring the MeasuringThat.net website, which provides a wealth of information on JavaScript benchmarking.
Related benchmarks:
js foreach vs map
Array loop vs foreach vs map -2
Array loop vs for...of vs map
Fastest iteration over array: map vs forEeach vs while vs for loop
Fastest iteration over array: map vs forEeach vs while vs for loop (fixed)
Comments
Confirm delete:
Do you really want to delete benchmark?