Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
List of Ids Get Object with Find Object lookup vs IndexOf lookup
(version: 0)
Comparing performance of:
indexOf vs find
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var objects = []; for (var i = 0; i < 10000; i++) { var idx = Math.floor(Math.random() * 1000); objects.push({ index: idx }); } var idx = Math.floor(Math.random() * 1000);
Tests:
indexOf
var objectIndices = []; for (var i = 0; i < 10000; i++) { objectIndices.push(objects[i].index); } var objectIdx = objectIndices.indexOf(idx); var tempResult = objects[objectIdx];
find
var tempResult = objects.find(v => v.index === idx);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
indexOf
find
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
2268.4 Ops/sec
find
375116.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what's being tested in this JavaScript microbenchmark. **Benchmark Definition** The benchmark is designed to compare two approaches: 1. **IndexOf Lookup**: This approach uses the `indexOf` method of an array to find the index of a specific element (in this case, the randomly generated `idx`) within the `objects` array. 2. **Find Method with Callback**: This approach uses the `find` method with a callback function to search for an object in the `objects` array that matches a certain condition (i.e., has an `index` property equal to `idx`). **Options Compared** The two approaches are being compared to determine which one is faster. The options being compared are: * Using the `indexOf` method of an array * Using the `find` method with a callback function **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **IndexOf Lookup** * Pros: + Well-established and widely supported method. + Fast, since it uses binary search under the hood. * Cons: + Can be slower than other methods for large datasets (in this case, 10,000 elements). + May not work as expected if the array is not sorted or if the index is negative. 2. **Find Method with Callback** * Pros: + More concise and expressive code. + Can be faster for some use cases, since it allows for early return and more efficient loop termination. * Cons: + Less well-established and widely supported method. + May require additional setup or configuration. **Library/Dependency** In this benchmark, the `find` method uses a callback function to search for an object in the array. The callback function takes two arguments: the current element being processed (`v`) and a boolean indicating whether the current element matches the desired condition (in this case, `v.index === idx`). This is a standard usage of the `find` method. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. The code is straightforward and uses only built-in methods and basic JavaScript concepts. **Other Alternatives** If you're interested in exploring other alternatives, here are a few options: 1. **Filter Method**: Instead of using `find`, you could use the `filter` method to create a new array with elements that match the desired condition. 2. **Loop-based Approach**: You could implement a loop-based approach using traditional JavaScript indexing and iteration to find the desired element in the array. 3. **Data Structures Other Than Arrays**: Depending on the specific requirements of your application, you might consider using other data structures, such as objects or trees, which can offer more efficient lookup mechanisms. Keep in mind that the choice of approach will depend on the specific use case and performance requirements of your application.
Related benchmarks:
findIndex vs indexOf on array of objs
Array.prototype.findIndex vs Array.prototype.map + Array.prototype.indexOf
indexof vs findindex with object equality
object[]: findIndex vs for loop with more complex condition check
Object arrays: findIndex vs for loop2
Comments
Confirm delete:
Do you really want to delete benchmark?