var a = { a: 1, b: 2, c: 3};
Object.values(a);
var a = { a: 1, b: 2, c: 3};
Object.keys(a).map(item => a[item])
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object values 2 | |
Keys to values 2 |
Test name | Executions per second |
---|---|
Object values 2 | 1620721.9 Ops/sec |
Keys to values 2 | 2154587.0 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
Benchmark Overview
The MeasureThat.net
website provides a platform for users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark that compares two approaches: using the new ES6 spread operator (Object.values()
) versus the traditional concat()
method.
What is being tested?
In this benchmark, we're comparing the performance of two approaches:
Object.values()
method to extract values from an object.Object.keys()
and then iterating over the resulting array to access the object's properties using property names (item => a[item]
).The test aims to measure which approach is faster.
Options Compared
In this benchmark, we're comparing two approaches:
Object.keys()
method to get an array of property names, followed by using the concat()
method to concatenate these property names with their corresponding values.Pros and Cons
Here's a brief analysis of each approach:
Library Used
In this benchmark, the Object.keys()
method is used from the built-in JavaScript object. No external libraries are required.
Special JS Feature or Syntax
There's no special JavaScript feature or syntax being tested in this benchmark. The focus is solely on comparing two approaches to extract values from an object.
Other Alternatives
If you're looking for alternative ways to extract values from an object, here are a few examples:
for...in
loop: This approach iterates over the object's properties using a for...in
loop.Array.from()
: This method converts an object into an array of its property names and then uses map()
to create a new array with the corresponding values.For example, using Array.from()
:
var obj = { a: 1, b: 2, c: 3 };
Array.from(Object.keys(obj)).map(item => obj[item]);
Keep in mind that these alternatives might have different performance characteristics compared to the approaches tested in this benchmark.