Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Loop test
(version: 0)
Comparing performance of:
Indexed for vs For in vs Backwards for vs Backwards while
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for(var i=0;i<1000;i++){ arr.push(i); }
Tests:
Indexed for
var sum = 0 for(var i=0,len = arr.length;i<len;i++){ sum += arr[i]; }
For in
var sum = 0 for(var a in arr){ sum += a; }
Backwards for
var sum = 0 for(var i=arr.length-1;i>=0;i--){ sum += arr[i]; }
Backwards while
var sum = 0 var i = arr.length; while(i--){ sum += arr[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Indexed for
For in
Backwards for
Backwards while
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 136 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Indexed for
237805.7 Ops/sec
For in
17583.8 Ops/sec
Backwards for
247280.7 Ops/sec
Backwards while
219775.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** MeasureThat.net is a platform that allows users to create and run JavaScript microbenchmarks. The provided benchmark definition json represents a simple loop test where an array of 1000 elements is created, and then three different loops are executed to calculate the sum of all elements in the array. The loops are: 1. Indexed for (using a traditional `for` loop with an index variable) 2. For in (using the `for...in` loop syntax to iterate over the array's properties) 3. Backwards for (using a traditional `for` loop with a decrementing index variable, but starting from the end of the array) 4. Backwards while (using a `while` loop with a decrementing counter) **Benchmark Preparation Code** The preparation code is a simple JavaScript snippet that creates an empty array and then populates it with 1000 elements using a `for` loop. ```javascript var arr = []; for (var i = 0; i < 1000; i++) { arr.push(i); } ``` **Individual Test Cases** Each test case represents one of the four loops mentioned earlier. The benchmark definition for each test case is a short JavaScript snippet that defines a loop and executes it. 1. **Indexed for**: `for (var i = 0, len = arr.length; i < len; i++) { sum += arr[i]; }` 2. **For in**: `for (var a in arr) { sum += a; }` 3. **Backwards for**: `for (var i = arr.length - 1; i >= 0; i--) { sum += arr[i]; }` 4. **Backwards while**: `var i = arr.length; while (i--){ sum += arr[i]; }` **Library and Special Features** None of the test cases use any external libraries or special features. The loops are basic JavaScript syntax. **Options Compared** The benchmark compares the performance of four different loop approaches: 1. Traditional `for` loop with an index variable 2. `for...in` loop syntax to iterate over the array's properties 3. Traditional `for` loop with a decrementing index variable, but starting from the end of the array 4. `while` loop with a decrementing counter **Pros and Cons** Here are some pros and cons of each approach: 1. **Indexed for**: Pros - efficient and well-supported by most browsers; Cons - can be verbose and hard to read when dealing with large arrays. 2. **For in**: Pros - concise and easy to write, but can lead to unexpected behavior if not used carefully; Cons - can be slower than traditional `for` loops due to the overhead of iterating over array properties. 3. **Backwards for**: Pros - efficient and easy to read, but requires a decrementing index variable; Cons - may not work correctly in all browsers or situations. 4. **Backwards while**: Pros - simple and concise, but can lead to confusion due to the use of a decrementing counter; Cons - may not be as efficient as traditional `for` loops. **Alternatives** Other alternatives for loop constructs include: 1. **Array.prototype.forEach()**: This method is more modern and concise than traditional `for` loops, but may be slower in older browsers. 2. **Generator functions**: These can be used to create loops that are both efficient and easy to read. 3. **Reduce() method**: This method can be used to calculate the sum of an array's elements without creating a loop. However, for this specific benchmark, the traditional `for` loop with an index variable remains the most efficient and widely supported approach.
Related benchmarks:
Array .push() vs .unshift(), 1M elements
Array .push() vs .unshift(), 100K elements
for-of over sliced array
Getting last element of array
Array last index
Comments
Confirm delete:
Do you really want to delete benchmark?