Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For loop vs Index Of (1000000 case)
(version: 0)
save length of the array in the variable vs get it the loop
Comparing performance of:
for loop vs indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var count = 1000000; for(var i = 0; i<count; i++) { arr.push(i); }
Tests:
for loop
function indexOfFor(obj, list) { var i; for (i = 0; i < list.length; i++) { if (list[i] === obj) { return i; } } return -1; }; indexOfFor(900000, arr);
indexOf
arr.indexOf(900000, arr);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for loop
indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0
Browser/OS:
Firefox 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for loop
1026.6 Ops/sec
indexOf
60.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases to understand what is being tested. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that compares two approaches: using a for loop and using the `indexOf` method of an array. * **Script Preparation Code**: The script creates an empty array (`var arr = []`) and initializes a variable `count` with 1,000,000. It then uses a for loop to push each number from 0 to `count-1` onto the array. * **Html Preparation Code**: This field is empty, which means no HTML preparation code is required for this benchmark. **Individual Test Cases** There are two test cases: 1. **For Loop** * The benchmark definition uses a custom function called `indexOfFor`, which implements a for loop to find the index of an element in the array. * The function iterates through the array using a for loop, checking each element's value against the target value (`obj`). 2. **IndexOf** * This test case uses the built-in `indexOf` method of JavaScript arrays to find the index of an element in the same array. **Library/External Functions** None are used explicitly in these benchmark definitions. However, it's worth noting that the `arr` variable is created using the `[]` syntax, which is a shorthand for creating an empty array. **JavaScript Features/Syntax** The benchmark uses JavaScript features like: * For loops (`for (i = 0; i < list.length; i++)`) * Array methods (`push`, `indexOf`) * Custom function definition (`function indexOfFor(obj, list) { ... }`) Now, let's discuss the pros and cons of each approach: **For Loop** Pros: * More control over the iteration process * Can be optimized for performance Cons: * May require more code than using built-in methods * Can be less concise and harder to read **IndexOf** Pros: * Built-in method, so it's likely to be implemented efficiently in the browser engine * Concise and easy to read * Often faster than a custom implementation due to optimizations by the browser engine Cons: * Less control over the iteration process * May not work as expected if the array is not ordered or if the element is not found **Other Alternatives** If you wanted to write a more traditional loop-based solution without using `indexOf`, you could use a while loop instead of a for loop: ```javascript function indexOfForLoop(obj, list) { var i = 0; while (i < list.length) { if (list[i] === obj) { return i; } i++; } return -1; } ``` Another alternative would be to use the `findIndex` method on the array, which is similar to `indexOf` but returns the index of the first element that matches the predicate: ```javascript function indexOfFindIndex(obj, list) { return list.findIndex((element) => element === obj); } ``` Keep in mind that these alternatives may not be as efficient or concise as using the built-in `indexOf` method.
Related benchmarks:
Reading array length inside vs outside for loop
Caching Uint8Array length property vs getting it each time in the loop
Caching length property vs getting it each time in the loop - ak
Caching length property vs getting it each time in the loop 22
Comments
Confirm delete:
Do you really want to delete benchmark?