Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Find Object ID vs IndexOf int
(version: 0)
Comparing performance of:
IndexOf vs find
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var indices = []; var objects = []; for (var i = 0; i < 10000; i++) { var idx = Math.floor(Math.random() * 1000); indices.push(idx) objects.push({ index: idx }); } var idx = Math.floor(Math.random() * 1000);
Tests:
IndexOf
var tempResult = indices.indexOf(idx);
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
5504708.0 Ops/sec
find
34352.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its various components. **Benchmark Definition** The benchmark measures two different approaches to find an object ID in a large array: 1. **IndexOf**: This approach uses the `indexOf` method of the array, which returns the index of the first occurrence of a specified value. 2. **find**: This approach uses the `find` method of the array, which returns the first element that satisfies the provided testing function. **Script Preparation Code** The script preparation code creates two large arrays: * `indices`: an array of 10,000 random integers * `objects`: an array of 10,000 objects with a single property `index`, which is also a random integer The script then generates a second random index `idx` to use in the benchmark. **Html Preparation Code** There is no HTML preparation code provided. **Library Usage** In both test cases, no libraries are explicitly used. However, it's worth noting that the `find` method uses a helper function (`v => v.index === idx`) under the hood, which is likely implemented using a library or engine-specific optimizations. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes explicitly used in these test cases. The benchmark focuses on the performance of basic array methods. **Options Compared** The two options being compared are: * **IndexOf**: uses the `indexOf` method, which returns the index of the first occurrence of a specified value. * **find**: uses the `find` method, which returns the first element that satisfies the provided testing function. **Pros and Cons** Here's a brief summary: * **IndexOf**: + Pros: widely supported, easy to understand, and well-documented. + Cons: may be slower than `find` for large datasets due to its linear search nature. * **find**: + Pros: more efficient for large datasets, as it uses a callback function to process the array elements. + Cons: less intuitive for some developers, and may have performance overhead depending on the implementation. **Other Alternatives** For similar use cases, other alternatives could be: * Using `Array.prototype.findIndex` (a more concise version of `indexOf`) * Implementing a custom binary search algorithm * Using a library like Lodash or Ramda for array processing Keep in mind that the performance differences between these approaches may vary depending on the specific use case and JavaScript engine. I hope this explanation helps! Let me know if you have any further questions.
Related benchmarks:
Object value comparison vs array of object value comparison with different loops v1
Lodash sort vs array.prototype.sort - compare with taking ids from different array
Object key access vs array index access 10000
Object key access vs array index access 100000
Object key access vs array index access 1000000
Comments
Confirm delete:
Do you really want to delete benchmark?