Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexof vs findindex with object equality
(version: 0)
Comparing performance of:
findindex vs indexof
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array(15000); var toFind = {id: 1} arr.fill({id: 0}) arr[Math.floor(Math.random() * 15000)] = toFind
Tests:
findindex
arr.findIndex(o => o === toFind)
indexof
arr.indexOf(toFind)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
findindex
indexof
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is comparing two ways to find an element in an array: `arr.indexOf(toFind)` and `arr.findIndex(o => o === toFind)`. The test case uses JavaScript and measures the performance of these two approaches on a large array (`15000` elements). **What's Being Tested?** * `arr.indexOf(toFind)`: + This method returns the index of the first occurrence of `toFind` in the array. + It has a time complexity of O(n), where n is the length of the array, because it needs to scan the entire array from start to end. * `arr.findIndex(o => o === toFind)`: + This method returns the index of the first element in the array that satisfies the condition `o === toFind`. + It has a time complexity of O(n) as well, but with a more efficient algorithm than `indexOf`. The function uses a binary search approach under the hood. + However, since JavaScript doesn't support `===` directly on objects (due to object reference sharing), it needs to use the `==` operator instead, which can lead to slower performance. **Pros and Cons** * `arr.indexOf(toFind)`: + Pros: Simple and widely supported. + Cons: Time complexity of O(n) makes it slower for large arrays. * `arr.findIndex(o => o === toFind)`: + Pros: More efficient algorithm, but requires support for the `===` operator on objects. + Cons: Less widely supported due to object reference sharing. **Library/Functionality Used** None mentioned in this specific benchmark. However, it's worth noting that `Array.prototype.indexOf()` and `Array.prototype.findIndex()` are built-in JavaScript methods that don't require any additional libraries. **Special JS Feature/Syntax** This benchmark doesn't use any special or experimental JavaScript features. **Other Alternatives** If you're concerned about the performance of `arr.indexOf(toFind)` for large arrays, you could consider: * Using a more efficient data structure, like a hash table (if possible). * Optimizing the search algorithm using techniques like caching or binary search. * Using a different programming language that supports faster lookup operations. Keep in mind that these alternatives would likely require significant changes to your codebase and may not be suitable for all use cases.
Related benchmarks:
findIndex vs indexOf on array of objs
findIndex vs indexOf - JavaScript performance
findIndex vs indexOf for simple array 2
Array.prototype.indexOf vs Array.prototype.findIndex
Comments
Confirm delete:
Do you really want to delete benchmark?