Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array access vs object access
(version: 0)
Comparing performance of:
array access vs object access
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
size = 15000 array = Array.from({ length: size }, (v, i) => i) object = Object.assign({}, Array.from({ length: size }, (v, i) => i))
Tests:
array access
const a = array[12000]
object access
const o = object[12000]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array access
object access
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Chrome OS 14541.0.0
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array access
41218208.0 Ops/sec
object access
39348584.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared, and other considerations. **Benchmark Definition** The provided JSON defines two benchmark tests: 1. `array access vs object access` 2. Two individual test cases: `array access` and `object access` **Script Preparation Code** The script preparation code generates two arrays and objects with 15,000 elements each: ```javascript size = 15000 array = Array.from({ length: size }, (v, i) => i) object = Object.assign({}, Array.from({ length: size }, (v, i) => i)) ``` This creates an array `array` and an object `object` with the same elements. **Individual Test Cases** The two individual test cases are: 1. `array access`: `const a = array[12000]` 2. `object access`: `const o = object[12000]` These test cases access specific elements in the arrays and objects, respectively. **Library: Object.assign** In the script preparation code, `Object.assign` is used to create the object `object`. This function creates a new object by copying all enumerable own properties from one or more source objects into a target object. In this case, it's used to copy the array elements into an object. **Special JS Feature: Array.from** The script preparation code uses `Array.from`, which is a JavaScript method that returns a new Array instance created with a specified constructor and arguments. This feature allows for concise creation of arrays from iterables like objects or arrays. **Comparison Approach** The benchmark compares two approaches: 1. **Array access**: Accessing an element directly in the array using bracket notation (`array[12000]`) 2. **Object access**: Accessing an element directly in the object using bracket notation (`object[12000]`) This comparison is likely meant to test the performance difference between accessing elements in arrays and objects. **Pros and Cons** * **Array access**: + Pros: Likely faster for large arrays, as array indices are contiguous and can be accessed more efficiently. + Cons: May lead to out-of-bounds errors if the index is invalid. * **Object access**: + Pros: More flexible, as object properties are not limited by contiguous indices. + Cons: Slower due to the overhead of searching for the property in the object. **Other Considerations** * The benchmark uses a relatively small array size (15,000 elements). Larger arrays may exhibit different performance characteristics. * The `object` is created using `Object.assign`, which may not be the most efficient way to create an object with many properties. * The test cases only access one element in each array and object. Testing multiple elements or more complex scenarios may provide a more accurate picture of performance. **Alternatives** There are other alternatives for creating arrays and objects, such as: * `Array.prototype.map` or `Array.prototype.forEach` to create an array with specific values * `Object.keys()` or `Object.entries()` to iterate over object properties and create a new object However, these alternatives may not provide the same level of performance optimization as using `Array.from` and `Object.assign`, respectively.
Related benchmarks:
Spread vs Object.assign (modify ) vs Object.assign (new)
object assign vs object spread on growing objects
Object assign vs empty obj
object spread vs Object.assign
JavaScript: Normal assignation VS Object.assign
Comments
Confirm delete:
Do you really want to delete benchmark?