Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object key access vs array finds
(version: 0)
Comparing performance of:
object vs map vs array
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var items = Array.from(Array(1000), (_,x) => ({key: x, value: x*10})); var objContainer = {}; var arrContainer = []; var mapContainer = new Map(); for (let i = 2000; i >= 0; i--) { const index = Math.floor(Math.random() * 1000); const item = items[index]; objContainer[item.key] = item; arrContainer.push(item); mapContainer.set(item.key,item); }
Tests:
object
items.forEach(item => objContainer[item.value])
map
items.forEach(item => mapContainer.get(item.value))
array
items.forEach(item => arrContainer.find(containerItem => containerItem.value === item.value))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
object
map
array
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark tests the performance of different ways to access data in JavaScript. **Options Compared:** * **Object (`object` test):** Uses an object where keys are unique identifiers (in this case, `item.key`) and values are the associated data objects (`item`). It accesses the data using key lookup (`objContainer[item.value]`). * **Map (`map` test):** Uses a Map, which is specifically designed for key-value storage and retrieval. It retrieves data by key using `mapContainer.get(item.value)`. * **Array with `find()` (`array` test):** Uses an array to store the data. To find a specific item, it iterates through the array using `find()` and compares values until a match is found (`arrContainer.find(containerItem => containerItem.value === item.value)`). **Pros and Cons:** * **Object:** * **Pro:** Can be very fast for direct key access if the keys are relatively evenly distributed. * **Con:** Performance degrades as the number of keys increases because JavaScript objects use hash tables, which can have collisions leading to slower lookups. * **Map:** * **Pro:** Designed for efficient key-based lookup, with average constant time complexity (O(1)) for get operations. * **Con:** Can be slightly more complex to work with than objects in some cases. * **Array with `find()`:** * **Pro:** Works well when you need to find an item based on a value rather than a unique key. * **Con:** `find()` typically iterates through the entire array, making it less efficient for large arrays compared to objects or Maps. **Other Considerations:** * **Data Structure Choice:** The best choice depends heavily on how you access and use your data. If you frequently need to look up items by a unique key, a Map is often the most efficient option. If you have smaller datasets and know the keys will be relatively evenly distributed, objects can be fast. * **Array Size:** For very large arrays (`find()` becomes slow), consider alternative methods like binary search if your data can be sorted. Let me know if you'd like a deeper dive into any particular aspect of this benchmark!
Related benchmarks:
Object keys vs Array map v2
Map key access vs array find
Object key access vs array find vs map
Object key access vs array find vs Set
Comments
Confirm delete:
Do you really want to delete benchmark?