Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object key access vs array find vs map
(version: 0)
Test speed of object access by key vs array vs map find to find object
Comparing performance of:
Object access vs Array find vs Map get
Created:
2 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 = []; var mapContainer = new Map(); 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); mapContainer.set(item.key, item) }
Tests:
Object access
items.every(item => objContainer[item.key])
Array find
items.every(item => arrContainer.find(containerItem => containerItem.key === item.key))
Map get
items.every(item => mapContainer.get(item.key))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object access
Array find
Map get
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object access
64249944.0 Ops/sec
Array find
10818732.0 Ops/sec
Map get
49783916.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested, along with the pros and cons of each approach. **Benchmark Overview** The benchmark tests three different ways to access an object in JavaScript: 1. **Object Key Access**: Directly accessing an object by its key using dot notation (`objContainer[item.key]`). 2. **Array Find**: Using the `find()` method on an array to find an object with a matching key. 3. **Map Get**: Using the `get()` method on a Map to retrieve an object with a matching key. **Pros and Cons of Each Approach** 1. **Object Key Access** * Pros: + Fastest, as it directly accesses the object's property. + Most efficient, as it avoids searching for the object in a collection. * Cons: + Assumes that the object is stored in an `objContainer` with a matching key. + May not work if the object is stored in a different container or doesn't have a matching key. 2. **Array Find** * Pros: + Works for any array, regardless of how it's structured. + Allows for flexible searching criteria (e.g., filtering by multiple keys). * Cons: + Slower than object key access, as it searches through the entire array. + May have performance issues with large arrays or complex search criteria. 3. **Map Get** * Pros: + Fast and efficient, similar to object key access. + Allows for fast lookup of objects by their keys in a Map. * Cons: + Assumes that the object is stored in a `mapContainer` with a matching key. + May not work if the object is stored in a different container or doesn't have a matching key. **Other Considerations** * The benchmark uses a fixed-size array of 1000 objects, which may not be representative of real-world scenarios where data sets can vary greatly. * The `items` array is created using `Array.from()` and `Array(1000)`, which creates a large array. This may impact performance due to JavaScript's limitations with working with very large arrays. * The benchmark doesn't account for potential caching or optimization techniques in modern browsers. **Libraries Used** In this benchmark, no external libraries are used beyond the built-in `Map` data structure in JavaScript. **Special JS Features/Syntax** None of the benchmark code uses special JS features or syntax that would affect its interpretation. However, it does use some advanced concepts like arrow functions (`() => {}`) and template literals (`\r\nitem.key = x * 10\r\n`). **Alternatives** There are several alternatives to these approaches: * Instead of using `Array.find()` for array-based searches, you could consider using a more efficient algorithm like binary search. * For Map-based searches, you could use other data structures like a balanced search tree or a hash table. * To improve performance in real-world scenarios, consider using caching techniques, lazy loading, and optimization strategies specific to your application's requirements.
Related benchmarks:
iterating from a filled object VS iterating from a map
Object key access vs array find 100 items yeh ok
Object key access vs array find vs Set
new Map vs set array to map
Comments
Confirm delete:
Do you really want to delete benchmark?