Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find vs findIndex vs filter
(version: 0)
Measuring which is faster
Comparing performance of:
Array.prototype.find vs Array.prototype.findIndex vs filter
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 1E5) arr[i] = i++;
Tests:
Array.prototype.find
const item = arr.find(item => item == 1E5);
Array.prototype.findIndex
const index = arr.findIndex(item => item == 1E5);
filter
const item = arr.filter(item => item == 1E5);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.prototype.find
Array.prototype.findIndex
filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0.1 Safari/605.1.15
Browser/OS:
Safari 26 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.find
15942.8 Ops/sec
Array.prototype.findIndex
15901.0 Ops/sec
filter
12564.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. **Benchmark Description** The benchmark is designed to measure the performance of three JavaScript methods: `Array.prototype.find()`, `Array.prototype.findIndex()`, and `filter()` (without using the prototype chain, i.e., a custom function). The goal is to determine which method is the fastest for finding an item with a specific value in an array. **Options Compared** The three options being compared are: 1. **`Array.prototype.find()`**: This method searches for the first occurrence of a specified value in an array and returns the element. 2. **`Array.prototype.findIndex()`**: This method finds the index of the first occurrence of a specified value in an array and returns it. If no such value is found, -1 is returned. 3. **`filter()` (without prototype chain)**: This method creates a new array with all elements that pass the test implemented by the provided function. **Pros and Cons** * `Array.prototype.find()`: Pros: * Efficient for finding a single item. * Uses binary search under the hood, making it relatively fast. Cons: * May not work well if the array is very large or sparse. * Only finds the first occurrence of the specified value. * `Array.prototype.findIndex()` Pros: * Returns the index of the found item, which can be useful for subsequent processing. Cons: * Returns -1 if no matching item is found, requiring additional checks. * May not be as efficient as `find()` in some cases (depending on browser implementation). * Custom `filter()` Pros: * More flexible and customizable than built-in methods. Con: * Requires manual implementation of the filtering logic, which can lead to more complex code. **Library Used** None. The benchmark uses only standard JavaScript functions. **Special JS Feature/Syntax (Not Applicable)** No special features or syntax are used in this benchmark. **Other Alternatives** If you wanted to test other methods for finding an item in an array, some alternatives could include: * `forEach()`: Loops through the array and checks each element individually. This can be slower than using `find()` or `filter()`. * `reduce()`: A method that reduces an array to a single value by applying a callback function to each element. It's not typically used for finding a specific item. * `some()`: Similar to `forEach()`, but returns a boolean indicating whether at least one element matches the specified condition. To test these alternatives, you would need to modify the benchmark definition and script preparation code accordingly. Keep in mind that the performance of these methods can vary depending on the browser, device, and specific use case.
Related benchmarks:
find vs findIndex (Array prototype methods)
find vs findindex with condition test
findIndex vs indexOf vs find vs filter - JavaScript performance
find vs indexOf (Array prototype methods)
Comments
Confirm delete:
Do you really want to delete benchmark?