Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object key access vs array index access 100000
(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(100000), (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:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object key access
185.5 Ops/sec
Array index access
321.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare the performance of accessing an item in two different ways: 1. **Object Key Access**: This involves accessing an item by its key in an object. 2. **Array Index Access**: This involves accessing an item by its index in an array. In the benchmark preparation code, a large array `items` is created with 100,000 items, each having a unique key-value pair. The script then creates two containers: an object container (`objContainer`) and an array container (`arrContainer`). It randomly selects one item from the `items` array and assigns it to both the object and array containers. **Options Compared** The benchmark is comparing the performance of accessing an item by its key in an object versus accessing an item by its index in an array. The options being compared are: * Object Key Access (using `objContainer[item.key]`) * Array Index Access (using `arrContainer[item.key]`) **Pros and Cons** Here's a brief analysis of the pros and cons of each approach: **Object Key Access:** Pros: * More intuitive and easier to read, especially for developers familiar with objects. * Can be more efficient if the object is sparse (i.e., has many empty keys). Cons: * May be slower due to the need to resolve the key lookup using `objContainer[item.key]`. * Requires additional memory allocation for the object container. **Array Index Access:** Pros: * Typically faster, as it directly accesses the array element at the specified index. * More memory-efficient, as only a single array is required. Cons: * Can be less intuitive and more prone to errors, especially for developers unfamiliar with arrays. * May not work correctly if the array has many empty slots. **Library Usage** In this benchmark, no external libraries are explicitly mentioned. However, some JavaScript features like `Array.from()` (used in the script preparation code) and optional chaining (`?.`) (used in the benchmark definitions) might be considered libraries by some developers. These features are part of the standard JavaScript language and are widely supported across most browsers. **Special JS Features/Syntax** There is no specific special JavaScript feature or syntax used in this benchmark, other than what's inherent to the language itself. **Other Alternatives** If you were to rewrite this benchmark, you could consider exploring alternative approaches, such as: * Using a data structure that combines both object and array features (e.g., a Map). * Optimizing the script preparation code for better performance. * Using more advanced profiling tools or benchmarking frameworks to gather more detailed results. Keep in mind that these alternatives would require significant modifications to the benchmark code and might not yield significantly different results.
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 1000000
Comments
Confirm delete:
Do you really want to delete benchmark?