Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object key access vs array index access
(version: 0)
Tests the speed of accessing an item by object key vs array index.
Comparing performance of:
Object key access vs Array index access
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var items = Array.from( Array(1000), (item, index) => ({ key: index, value: index*10 }) ); var objContainer = new Object(); var arrContainer = new Array(items.length); // For a tenth of the number of items... for (let i = items.length/10; i >= 0; i--) { // Choose a random item const index = Math.floor(Math.random() * items.length); const item = items[index]; // Assign the item to the object and array containers objContainer[item.key] = item; arrContainer[item.key] = item; }
Tests:
Object key access
items.forEach((item) => objContainer[item.key]?.value)
Array index access
items.forEach((item) => arrContainer[item.key]?.value)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object key access
Array index access
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:148.0) Gecko/20100101 Firefox/148.0
Browser/OS:
Firefox 148 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object key access
78508.2 Ops/sec
Array index access
76704.4 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares the performance of accessing items using object keys versus array indices in JavaScript. **Options Compared:** * **Object key access:** Using bracket notation (`objContainer[item.key]`) to retrieve values from an object. * **Array index access:** Directly accessing elements using their numerical index (`arrContainer[item.key]`). **Pros and Cons:** * **Object Key Access:** * **Pros:** More flexible as keys can be any data type (strings, numbers, objects). Easier to organize data with meaningful identifiers. * **Cons:** Can be slower than array indexing due to potential hash table lookups. * **Array Index Access:** * **Pros:** Generally faster because elements are stored contiguously in memory. Direct access using indices is very efficient. * **Cons:** Less flexible, keys must be numerical and sequential. Less intuitive for representing real-world data relationships. **Considerations:** * **Data Structure Choice:** The choice depends on your data. If you need flexibility and meaningful identifiers, use objects. If you have a fixed set of items with numerical relationships, arrays are better. * **Performance Trade-offs:** In most cases, the performance difference between these two approaches is negligible unless dealing with very large datasets. **Other Alternatives:** * **Maps:** Similar to objects but provide faster lookups due to hash table implementation. Useful when you need efficient key-value storage and retrieval. Let me know if you have any other questions!
Related benchmarks:
Object key access vs array find vs Set
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?