Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs for loop test
(version: 0)
Comparing performance of:
findIndex vs for loop
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = new Array(15000); arr.fill({ id: 0 }); arr = arr.map((el, idx) => el.id = idx); var foo = Math.floor(Math.random() * 15000);
Tests:
findIndex
var index = arr.findIndex((num) => num === foo);
for loop
var index = -1 for(var i = 0; i < arr.length; i++) { if(arr[i].id === index) { index = i; 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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare two approaches for finding an index in an array: `findIndex` (using the `Array.prototype.findIndex()` method) versus a traditional `for` loop. The test creates a large array of 15,000 elements with `id` properties and fills them with random values. **Options Compared** Two options are being compared: 1. **`findIndex`**: Uses the `Array.prototype.findIndex()` method to find the index of the first element that satisfies the provided condition. 2. **Traditional `for` loop**: Iterates over the array using a traditional `for` loop, checking each element's `id` property until it finds a match. **Pros and Cons** * **`findIndex`**: + Pros: Concise, efficient, and easy to read. + Cons: May have performance issues if the input array is large or the condition is complex, since it needs to iterate over the entire array. * **Traditional `for` loop**: + Pros: Can be faster for very large arrays due to its control over the iteration process. + Cons: More verbose and error-prone, as it requires manual indexing and condition checking. **Other Considerations** The benchmark also includes the browser version (Chrome 103), device platform (Desktop), operating system (Windows), and executions per second metrics. This information helps to identify variations in performance across different environments. **Library Used** There is no explicit library mentioned, but `Array.prototype.findIndex()` is a part of the JavaScript standard library. **Special JS Feature/Syntax** No special features or syntax are being tested in this benchmark. However, if you're interested in exploring other areas, here are some alternatives: * Other array methods: `forEach()`, `map()`, `filter()`, etc. * Modern ES6/ES7 features like arrow functions and template literals * Other performance optimizations, such as using Web Workers or parallel processing If you'd like to explore these topics further, feel free to ask!
Related benchmarks:
findIndex vs indexOf - JavaScript performance
Object arrays: findIndex vs for loop2
Object arrays: findIndex vs for loop (length cached)
findIndex vs IndexOf + map
findIndex vs for loop with plain number array
Comments
Confirm delete:
Do you really want to delete benchmark?