Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Find Object ID vs IndexOf int take 2
(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 }); }
Tests:
IndexOf
var idx1 = Math.floor(Math.random() * 1000); var tempResult = indices.indexOf(idx1);
find
var idx2 = Math.floor(Math.random() * 1000); var tempResult = objects.find(v => v.index === idx2);
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
418826.1 Ops/sec
find
122598.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain what's being tested in the provided JSON benchmark. The benchmark is designed to compare two different approaches for finding an object with a specific property value in an array: using the `indexOf()` method and using the `find()` method. **IndexOf Method** The `indexOf()` method returns the index of the first occurrence of a specified element within an array. In this case, we're searching for a random integer between 0 and 999 in the `indices` array. Pros: * Wide support: `indexOf()` is a standard method in JavaScript, supported by most browsers and environments. * Fast lookup: `indexOf()` uses a linear search algorithm, which can be efficient for small arrays or when the element is likely to be present near the start of the array. Cons: * Returns -1 if not found: This means that if the element is not present in the array, `indexOf()` returns -1. In some cases, this might be considered an error. * Can be slow for large arrays: As the array size increases, `indexOf()`'s linear search algorithm can become slower. **Find Method** The `find()` method returns the first element in an array that satisfies a provided testing function. In this case, we're using an arrow function to check if the object's `index` property matches the random integer. Pros: * Returns undefined if not found: Unlike `indexOf()`, `find()` explicitly returns `undefined` when no matching element is found. * Can be faster for large arrays: `find()` uses a binary search algorithm, which can be more efficient than linear search for larger arrays. Cons: * Not as widely supported: While `find()` is also a standard method in JavaScript, it's not supported by older browsers or environments that only support array methods introduced later (e.g., ECMAScript 2015+). **Library Used** In this benchmark, the `indices` and `objects` arrays are created using native JavaScript code. No external libraries are used. **Special JS Feature/Syntax** There are no special features or syntaxes being tested in this benchmark that require specific knowledge of JavaScript. The focus is on comparing two different approaches for finding an object with a specific property value in an array. **Other Alternatives** For those interested in exploring other alternatives, here are a few options: * `Array.prototype.includes()`: Similar to `indexOf()`, but returns a boolean result instead of the index. * `Array.prototype.reduce()`: Can be used to find the first matching element by accumulating the results of each iteration until one matches. * Custom implementation using bitwise operations or other algorithms: Depending on the specific requirements, it might be possible to implement an even more efficient solution using low-level optimizations. Keep in mind that these alternatives might not be as widely supported or performant as `indexOf()` and `find()`, which are designed for performance and compatibility.
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?