Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object key access vs array index access 1000000
(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:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var items = Array.from( Array(1000000), (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 year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object key access
80.1 Ops/sec
Array index access
82.1 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 compares two approaches to accessing an item in an array: 1. **Object key access**: Accessing an item using its object key (e.g., `objContainer[item.key]`). 2. **Array index access**: Accessing an item using its array index (e.g., `arrContainer[item.key]?.value`). The benchmark creates a large dataset of objects with unique keys and values, and then assigns each object to both an object container and an array container. **Pros and Cons of Each Approach** **Object Key Access:** Pros: * Can be more readable and maintainable when working with complex data structures. * Allows for easy access to nested properties using dot notation (e.g., `objContainer[item.key].subproperty`). Cons: * Can lead to slower performance due to the overhead of object lookup and potential hash table collisions. * May require additional memory to store the objects. **Array Index Access:** Pros: * Can be faster for large datasets, as it avoids the overhead of object lookup and hash table collisions. * Requires less memory than storing objects in an array container. Cons: * Can be less readable and maintainable when working with complex data structures. * May require additional logic to access nested properties. **Other Considerations** In this benchmark, both approaches have their trade-offs. The object key access approach is more readable but potentially slower, while the array index access approach is faster but less readable. The choice of which approach to use depends on the specific requirements and constraints of the project. **Library/ Framework Usage** The test case uses JavaScript's built-in `Array` and `Object` types, as well as the optional chaining operator (`?.`) in a recent version of JavaScript (ECMAScript 2020+). **Special JS Features/Syntax** This benchmark does not use any special JavaScript features or syntax, such as async/await, arrow functions, or generators. **Alternatives** If you were to implement this benchmark from scratch, you could consider using alternative approaches, such as: * Using a library like Lodash to optimize object lookup and iteration. * Implementing a custom caching mechanism to reduce the overhead of repeated lookups. * Using a more specialized data structure, such as a trie or a hash table, to improve performance. However, for most use cases, the JavaScript built-in types and operators are sufficient, and any optimizations should focus on optimizing the specific parts of the code that are performance-critical.
Related benchmarks:
Object key access vs array index access
Object key access vs array find vs Set
Object key access vs array index access 10000
Object key access vs array index access 100000
Comments
Confirm delete:
Do you really want to delete benchmark?