Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.find vs object by key
(version: 0)
Comparing performance of:
Array.find vs Object by key
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arrayExample = Array(40).fill().map((_, idx) => ({ id: idx })); var objExample = arrayExample.reduce((prev, curr) => prev[curr.id] = curr);
Tests:
Array.find
const found = arrayExample.find((item) => item.id === 39);
Object by key
const found = objExample[39];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.find
Object by key
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.find
22999624.0 Ops/sec
Object by key
174148608.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. The benchmark is comparing two approaches to find an element in an array or object: `array.find` and accessing an object by its key. **Array.find** The first test case uses the `find` method on an array. The `find` method returns the first element in the array that satisfies the provided testing function. In this case, the testing function is `(item) => item.id === 39`, which means it checks if the `id` property of each object in the array is equal to 39. **Object by key** The second test case accesses an object by its key directly. The `objExample` object is created from the `arrayExample` array using the `reduce` method, where each element in the original array is used as a key to add the corresponding value to the resulting object. **Options compared** The benchmark is comparing two options: 1. Using `array.find`: This approach uses the `find` method on the array, which can return immediately if it finds an element that satisfies the testing function. 2. Accessing an object by its key: This approach directly accesses the object using the provided key. **Pros and cons of each approach** **Array.find** Pros: * Can be more efficient if the array is large and only one element needs to be found. * Can return immediately if it finds the desired element. Cons: * May not be as efficient for small arrays or for accessing specific elements by index. * Requires a function to specify the testing criteria, which can add complexity. **Object by key** Pros: * Can access elements by their exact key without relying on functions or indexing. * Generally faster for direct accesses since it's an O(1) operation. Cons: * May require additional memory allocation and processing if using objects with nested structures. **Library usage** In this benchmark, no specific libraries are used other than the built-in `Array` and `Object` methods in JavaScript. However, the `reduce` method is used to create the object from the array, which is a common pattern in functional programming. **Special JS features or syntax** This benchmark does not use any special JavaScript features or syntax that require specific knowledge of modern JavaScript. It's a straightforward comparison of two basic methods for accessing elements in arrays and objects. **Other alternatives** If you need to find an element in an array or object, other alternatives might include: * `array.indexOf()` (which returns -1 if not found) * Using a regular expression with the `match` method * Iterating over the array or object manually using loops However, these approaches are generally less efficient than using the `find` method for arrays and direct accesses for objects.
Related benchmarks:
Object.fromEntries w/Array.map vs Array.reduce
Object.keys().length vs Map.size
Object.fromEntries(Array.map) vs Array.reduce (with different methods)
array find vs object key
Comments
Confirm delete:
Do you really want to delete benchmark?