Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
While vs Find vs FindIndex
(version: 0)
Comparing performance of:
Find vs Find Index vs While
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var numbers = []; for (let i = 0; i < 100000; i++) { numbers.push(i); }
Tests:
Find
const item = numbers.find((num) => num === 90000);
Find Index
const item = numbers.findIndex((num) => num === 90000);
While
let i = 0; while(i < numbers.length && numbers[i] !== 90000) i++;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Find
Find Index
While
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/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Find
3225.7 Ops/sec
Find Index
3123.8 Ops/sec
While
106.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches: `while`, `find()`, and `findIndex()` in JavaScript. The test case consists of a script that generates an array of 100,000 numbers and then uses each approach to find a specific number (90000) within the array. **Options Compared** The benchmark compares the following options: 1. **While Loop**: A traditional `while` loop that increments a counter until it finds the target value. 2. **Find() Method**: The `find()` method, which returns the first element in the array that satisfies the provided callback function. 3. **FindIndex() Method**: The `findIndex()` method, which returns the index of the first element in the array that satisfies the provided callback function. **Pros and Cons** * **While Loop** * Pros: Can be more intuitive and easier to understand for some developers, as it explicitly controls the loop condition. * Cons: May have higher overhead due to the explicit increment operation, and it's less concise than other methods. * **Find() Method** * Pros: More concise and readable than `while`, especially when working with callback functions. It's also more efficient since it stops searching as soon as it finds a match. * Cons: May have higher overhead due to the function call and potential intermediate results, which can be slow for large arrays. * **FindIndex() Method** * Pros: Similar to `find()` but returns an index instead of the actual value. It's also more efficient since it only needs to iterate until a match is found. * Cons: May not be as intuitive or readable as `find()` for some developers, especially those who are new to JavaScript. **Library/Feature Used** The benchmark uses the following libraries/features: * **Callback functions**: The `find()`, `findIndex()`, and `while` loops all use callback functions. These functions are executed in sequence until a match is found. * **Array methods**: All three approaches manipulate arrays, which are built-in JavaScript data structures. **Special JS Features/Syntax** This benchmark does not explicitly utilize any special JavaScript features or syntax, but it assumes the presence of modern JavaScript engines and their support for array methods and callback functions.
Related benchmarks:
findIndex vs indexOf for simple array 2
find vs findIndex vs for in (Array prototype methods)
Object arrays: findIndex vs for loop2
findIndex vs for loop with plain number array fixed
Comments
Confirm delete:
Do you really want to delete benchmark?