Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs map
(version: 0)
Comparing performance of:
findindex vs map
Created:
8 years ago
by:
Guest
Jump to the latest result
Tests:
findindex
var test = [{name: 'test1', id: 1}, {name: 'test2', id: 2}, {name: 'test3', id: 3}]; var index = test.findIndex(i => i.name === 'test3');
map
var test = [{name: 'test1', id: 1}, {name: 'test2', id: 2}, {name: 'test3', id: 3}]; var index = test.map(t => t.name).indexOf('test3');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
findindex
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findindex
32638594.0 Ops/sec
map
17422824.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The provided benchmark compares two approaches: using `findIndex` and using `map` with `indexOf`. The test cases are designed to measure the performance of these methods in finding a specific element in an array. **Test Cases** There are two test cases: 1. **findIndex** ```javascript var test = [{name: 'test1', id: 1}, {name: 'test2', id: 2}, {name: 'test3', id: 3}]; var index = test.findIndex(i => i.name === 'test3'); ``` This code uses the `findIndex` method to find the index of the first element in the array that matches the condition. 2. **map** (with `indexOf`) ```javascript var test = [{name: 'test1', id: 1}, {name: 'test2', id: 2}, {name: 'test3', id: 3}]; var index = test.map(t => t.name).indexOf('test3'); ``` This code uses the `map` method to create a new array with only the `name` property of each element, and then uses the `indexOf` method to find the index of `'test3'`. **What's Being Tested** The benchmark is testing: * The performance difference between using `findIndex` and `map` (with `indexOf`) for finding an element in an array. **Library/Functions Used** * `findIndex`: This function returns the index of the first element that satisfies the provided condition. * `map`: This function creates a new array with the results of applying the provided callback function to each element of the original array. * `indexOf`: This function returns the index of the first occurrence of a specified value in an array. **Special JS Features/Syntax** None mentioned explicitly, but note that the use of arrow functions (`i => i.name === 'test3'`) and template literals (`var test = [...];`) is part of modern JavaScript syntax. **Pros and Cons of Each Approach** * **findIndex**: + Pros: More concise and efficient for finding a single element. + Cons: May not be suitable if the condition involves multiple steps or complex logic. * **map** (with `indexOf`): + Pros: Can be useful when you need to perform some transformation on each element before searching, but it's less efficient than `findIndex` in this case. + Cons: Requires creating a new array and then searching for the value, which can be slower. **Other Considerations** * The use of `map` with `indexOf` is not typically considered the most efficient approach, as it creates an unnecessary intermediate array and requires two passes through the data. * If you need to find multiple elements in the array, using `findIndex` or another more suitable method (like `filter`) would be a better choice. **Alternatives** Other methods for finding an element in an array include: * `forEach`: Similar to `map`, but it doesn't return a value. * `every`, `some`, and other aggregate functions: These can be used with callback functions to find elements that match certain conditions, but they may not be as efficient as `findIndex` or `filter`. * `Array.prototype.find()`: This is a newer method introduced in ECMAScript 2019, which returns the first element that satisfies the provided condition. Keep in mind that the best approach depends on the specific requirements of your use case.
Related benchmarks:
findIndex vs map & indexOf
index vs lastindexof empty
findIndex vs map & indexOf vs find
findIndex vs IndexOf + map
findIndex vs indexOf - JavaScript performance v2
Comments
Confirm delete:
Do you really want to delete benchmark?