Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values vs array index loop
(version: 0)
Comparing performance of:
Object values vs Array values
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,"10":10}; var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
Tests:
Object values
Object.values(obj).map((a) => a );
Array values
arr.map((a) => a);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object values
Array values
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Android 14; Mobile; rv:138.0) Gecko/138.0 Firefox/138.0
Browser/OS:
Firefox Mobile 138 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object values
2914647.0 Ops/sec
Array values
6622147.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **What is being tested?** The test case compares two approaches to accessing and processing an array of numbers: 1. **Array index loop**: This approach uses traditional indexing to access each element in the array, like `arr[0]`, `arr[1]`, ..., `arr[9]`. 2. **Object values using Object.values()**: This approach uses the `Object.values()` method to retrieve an array of values from the object, which is then mapped over. **Options compared** The two approaches have different performance characteristics: * Array index loop: + Pros: Generally faster and more efficient for small arrays. + Cons: Can be slower and less memory-efficient for large arrays, as it requires explicit indexing and potentially more allocations. * Object values using Object.values(): + Pros: Faster and more memory-efficient for large arrays, since `Object.values()` can handle arbitrary data structures and doesn't require explicit indexing. + Cons: May be slower for small arrays or when the object has a large number of properties, as it requires additional overhead to retrieve the property values. **Library and its purpose** The `Object.values()` method is a built-in JavaScript method introduced in ECMAScript 2019. It takes an object as input and returns an array of its property values. This method is useful when you need to work with objects that have a large number of properties or complex data structures. **Special JS feature or syntax** There are no special features or syntaxes mentioned in this benchmark, but it's worth noting that the `Object.values()` method relies on the ECMAScript 2019 specification and may not be supported in older browsers or environments. **Alternative approaches** If you're looking for alternative approaches to accessing array elements or object properties, some options include: * Using a loop with the `for...of` statement: `for (const value of arr) { /* process value */ }` * Using an arrow function with `map()`: `(arr.map((value) => { /* process value */ }))` * Using a library like Lodash or Ramda, which provide utility functions for working with arrays and objects. Overall, this benchmark is designed to compare the performance of two approaches to accessing array elements: traditional indexing versus using `Object.values()` to retrieve property values. The test case is well-suited for comparing the performance of these approaches in a controlled environment.
Related benchmarks:
for.. of vs forEach
push vs. Index write performance
array.splice vs array.length
For in vs Object.*.forEach vs Object.values vs _.forEach(_.values vs n=arr.length
array.length = 0 vs []
Comments
Confirm delete:
Do you really want to delete benchmark?