Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values vs Reduce
(version: 0)
Comparing performance of:
Object.values vs Keys, Reduce
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
Object.values
var a = { a: 1, b: 2, c: 3}; return Object.values(a);
Keys, Reduce
var a = { a: 1, b: 2, c: 3}; return Object.keys(a).reduce((prev, key) => { prev.push(a[key]); return prev; },[]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.values
Keys, Reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.values
19476534.0 Ops/sec
Keys, Reduce
23447594.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark compares the performance of two approaches to access values in an object: 1. `Object.values` 2. Using `Object.keys` with the `reduce()` method **What are Object.values and Object.keys?** `Object.values` returns an array of a given object's own enumerable property values. `Object.keys` returns an array of a given object's own enumerable property keys (i.e., names). The `reduce()` method applies a function to each element in the array and reduces it to a single value. In this case, we're using it to concatenate the values from the `Object.keys(a)` array into a single array. **Options Compared** We have two options being compared: 1. **`Object.values`**: Directly access the object's values as an array. 2. **Keys, Reduce**: Use `Object.keys()` and then apply `reduce()` to concatenate the key names into an array of values. **Pros and Cons of Each Approach** * **`Object.values`**: + Pros: Simple, efficient, and straightforward. + Cons: Requires that the object has a numerical property (i.e., its keys must be numbers), which might not always be the case. * **Keys, Reduce**: + Pros: Works with objects that have non-numerical keys. However, it's more complex and potentially slower than `Object.values`. + Cons: Requires two additional function calls (`Object.keys()` and `reduce()`). **Other Considerations** When choosing between these approaches, consider the following: * If you know that your object will always have numerical keys, `Object.values` is likely a better choice. * If you need to handle objects with non-numerical keys, `Keys, Reduce` might be a better option. **Library and Special JS Features** Neither of these approaches relies on any specific library or special JavaScript features beyond what's built into the language.
Related benchmarks:
Object.fromEntries on array vs reduce on array
Object.fromEntries vs reduce v21
Object.fromEntries vs reduce round 2
Flatmap vs reduce with objects
Object.fromEntries vs reduce object.assign vs for in
Comments
Confirm delete:
Do you really want to delete benchmark?