Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object key access vs array find
(version: 0)
Test speed of object access by key vs array find to find object
Comparing performance of:
Object access vs Array find
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var items = Array.from(Array(1000), (_,x) => ({key: x, value: x*10})); var objContainer = {}; var arrContainer = []; for (let i = 100; i >= 0; i--) { const index = Math.floor(Math.random() * 1000); const item = items[index]; objContainer[item.key] = item; arrContainer.push(item) }
Tests:
Object access
items.forEach(item => objContainer[item.value])
Array find
items.forEach(item => arrContainer.find(containerItem => containerItem.value === item.value))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object access
Array find
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object access
1907333.8 Ops/sec
Array find
1675.6 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares two ways to find an object within a collection: accessing it directly through an object key and using the `find` method on an array. **Object Access:** * **Benchmark Definition:** `items.forEach(item => objContainer[item.value])` * This approach iterates through each item in the 'items' array. For every item, it uses the `item.value` as a key to access and potentially modify an object (`objContainer`). **Array Find:** * **Benchmark Definition:** `items.forEach(item => arrContainer.find(containerItem => containerItem.value === item.value))` * This approach also iterates through each item in the 'items' array. However, for every item, it uses the `find` method on the `arrContainer` array to locate an object whose `value` property matches the current item's `value`. **Pros and Cons:** | Approach | Pros | Cons | |--------------|--------------------------------------------|--------------------------------------------------| | **Object Access** | Potentially faster for retrieving a specific value if you know the key in advance. | Requires knowing the object's key structure beforehand. Less flexible than array-based searching.| | **Array Find** | More flexible, can search based on any property. Can handle dynamic data structures more easily. | Potentially slower than direct object access for frequent lookups using known keys. | **Other Considerations:** * **Data Structure:** The choice between these approaches depends heavily on the nature of your data. If you have a well-defined structure with predictable keys, object access might be faster. If your data is more dynamic or you need to search based on various criteria, `find` offers more flexibility. * **Frequency of Access:** For very frequent lookups using known keys, object access can be significantly faster. However, if the search criteria are complex or change frequently, `find` might be a better choice. **Alternatives:** Besides these two methods, consider: * **Hash Tables (Map):** If you need to perform extremely fast key-value lookups, hash tables provide near constant time complexity for both insertion and retrieval. * **Libraries like Lodash:** Libraries like Lodash offer optimized `find` implementations that might be faster than the built-in array method. Let me know if you have any other questions!
Related benchmarks:
Object key access vs array find vs map
Object key access vs array find 100 items yeh ok
Object key access vs array find vs Set
Object key access vs array index access 1000000
Comments
Confirm delete:
Do you really want to delete benchmark?