Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs for loop with plain number array
(version: 0)
Testing finding an object array via findIndex or by using a for loop
Comparing performance of:
findIndex vs for loop
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(15000); arr.fill([ 500 ]); arr = arr.map((el, idx) => el.id = idx); var foo = Math.floor(Math.random() * 15000);
Tests:
findIndex
arr.findIndex((itm) => itm.id === foo);
for loop
for (let i = 0; i < arr.length; i++) { if (arr[i].id === foo) break; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
findIndex
for loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15
Browser/OS:
Safari 17 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findIndex
134.5 Ops/sec
for loop
55.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the Benchmark Definition and test cases. **Benchmark Overview** The benchmark is designed to compare two approaches for finding an object in a large array: 1. Using the `findIndex` method with a callback function. 2. Using a traditional `for` loop. **Options Compared** The two options being compared are: * `arr.findIndex((itm) => itm.id === foo);`: This is the `findIndex` method, which returns the index of the first element in the array that satisfies the provided condition. * `for (let i = 0; i < arr.length; i++) { ... }`: This is a traditional `for` loop that iterates through the array and checks each element's `id` property. **Pros and Cons** * **findIndex method:** + Pros: - More concise and readable code. - Less prone to errors due to the use of a callback function. + Cons: - May be slower than traditional loops due to the overhead of the `findIndex` method. - Requires JavaScript 5 or later for syntax support. * **Traditional `for` loop:** + Pros: - Faster execution speed, as it avoids the overhead of the `findIndex` method. - More control over the iteration process. + Cons: - More verbose and error-prone code due to the use of an explicit loop. **Library** In this benchmark, no libraries are explicitly mentioned. However, the `findIndex` method is a built-in JavaScript function that uses the V8 engine (specifically, SpiderMonkey) for its implementation. **Special JS Feature or Syntax** The benchmark utilizes the `let` and `const` keywords with block scope, which was introduced in ECMAScript 2015 (ES6). The `=>` symbol is used as an arrow function syntax. These features are not essential to understanding the benchmark but demonstrate modern JavaScript capabilities. **Other Alternatives** If you wanted to write a similar benchmark for this scenario, you could also use: * `Array.prototype.some()`: This method returns `true` if at least one element in the array satisfies the condition. * Using a regular expression with `String.prototype.indexOf()` or `String.prototype.search()` * Utilizing `Buffer.find()` (for Node.js and other platforms that support it)
Related benchmarks:
Object arrays: findIndex vs for loop
Object arrays: findIndex vs for loop2
Object arrays: findIndex vs for loop (length cached)
Object arrays: findIndex vs for loop vs some
Comments
Confirm delete:
Do you really want to delete benchmark?