Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
benchmark for time Array access vs Object access 2
(version: 0)
Comparing performance of:
Array vs Object
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var items = Array.from(Array(100), (_, x) => ({ key: x, value: x * 10 })); var objContainer = {}; var arrContainer = []; for (let i = 0; i <= 100; ++i) { const item = items[i]; objContainer[i] = item; arrContainer.push(item); }
Tests:
Array
items.forEach((_, idx) => arrContainer[idx])
Object
items.forEach((_, idx) => objContainer[idx])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array
Object
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array
178695.6 Ops/sec
Object
179546.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares the performance of accessing elements in an array versus an object using JavaScript. The script preparation code creates two containers: `objContainer` (an object) and `arrContainer` (an array). It then populates both containers with 100 items, where each item has a unique key-value pair. **Test Cases** There are two test cases: 1. **Array**: The benchmark definition uses the `forEach` method to iterate over the array, accessing elements using their index (`idx`). This is equivalent to using `arrContainer[idx]`. 2. **Object**: Similarly, this test case uses `forEach` to iterate over the object, accessing elements using their key (`idx`). This is equivalent to using `objContainer[idx]`. **Comparison** The benchmark compares the performance of these two approaches on an object and an array. **Pros and Cons** * **Array Access** + Pros: - Typically faster for arrays because indexes are numerical and can be directly accessed. - Less overhead compared to accessing objects, as there's no need to look up keys using `in` or bracket notation (`[]`). + Cons: - Limited to numerical indexes; if you need to access elements by non-numerical key (e.g., string), this approach won't work. * **Object Access** + Pros: - More flexible and can be used for accessing elements by any type of key, including strings, numbers, or even other objects. + Cons: - Typically slower due to the overhead of looking up keys in an object. **Library/Functionality** There is no explicit library being used here, but `forEach` is a built-in JavaScript method. If you were to use a third-party library for similar functionality, it might be a polyfill or an alternative implementation that provides better performance or additional features. **Special JS Feature/Syntax** There are no special JavaScript features or syntax being tested in this benchmark. However, if we consider the `forEach` loop as a whole, it's using a modern JavaScript feature (`for...of` loops were introduced in ECMAScript 2015).
Related benchmarks:
Array construct vs array push
Array Push vs. Index Access
Object.fromEntries vs create temp object
Object.fromEntries vs create temp object vs Array.reduce
Array.forEach vs Object.keys().forEach
Comments
Confirm delete:
Do you really want to delete benchmark?