Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findIndex vs map and create new array
(version: 0)
Comparing performance of:
findindex vs map
Created:
2 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.id === 3); const updatedObj = { ...test[index], name: 'REPLACED' }; const result = [ ...test.slice(0, index), updatedObj, ...test.slice(index + 1) ];
map
var test = [{name: 'test1', id: 1}, {name: 'test2', id: 2}, {name: 'test3', id: 3}]; var index = test.map(t => t.id).indexOf(3); const updatedObj = { ...test[index], name: 'REPLACED' }; const result = [ ...test.slice(0, index), updatedObj, ...test.slice(index + 1) ];
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:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
findindex
11662327.0 Ops/sec
map
9959783.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided JSON represents two test cases for JavaScript microbenchmarks on MeasureThat.net. The goal of these tests is to compare the performance of two approaches: using `findIndex` and using `map` with `indexOf`. **Options Compared** The main options being compared are: 1. **FindIndex**: Uses the `findIndex` method to find the index of an element in an array that satisfies a provided condition. 2. **Map and indexOf**: Uses the `map` function to create a new array with transformed elements, then uses the `indexOf` method to find the index of the desired element. **Pros and Cons** **FindIndex Approach** Pros: * Shorter code: The `findIndex` approach requires less code compared to using `map` and `indexOf`. * Potential performance advantage for small arrays or sparse arrays. Cons: * May be slower for large arrays due to the potential need for a linear search. * Requires careful handling of edge cases, such as when no element matches the condition. **Map and indexOf Approach** Pros: * Easier to handle edge cases, as `indexOf` can return -1 if not found. * Can be more efficient for large arrays using a binary search algorithm. Cons: * Longer code: The `map` and `indexOf` approach requires more code compared to using `findIndex`. * Potential performance disadvantage for very small arrays or sparse arrays due to the overhead of creating a new array. **Library Usage** In both test cases, no libraries are explicitly mentioned. However, the use of modern JavaScript features like template literals (`\r\n`) and spread syntax (`{ ...test[index], name: 'REPLACED' }`) suggests that the tests target a relatively recent version of JavaScript ( likely ECMAScript 2015 or later). **Special JS Features** The `findIndex` method was introduced in ECMAScript 2012, while the `map` and `indexOf` combination was used before those methods existed. The use of these features indicates that the tests target a modern JavaScript environment. **Alternatives** Other alternatives for finding an element's index in an array include: 1. **forEach**: Iterate through the array and keep track of indices manually. 2. **Array.prototype.findIndexAsync**: Similar to `findIndex`, but asynchronous. 3. **Binary search algorithms**: For very large arrays, using a binary search algorithm can be more efficient. Keep in mind that these alternatives may not offer performance advantages over the `findIndex` or `map` and `indexOf` approaches for most use cases.
Related benchmarks:
findIndex vs map & indexOf
findIndex vs includes vs map & indexOf
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?