const obj = {a: 'a', b: 'b', c: 'c'};
const {a, b, c } = obj
console.log(a, b, c);
const a = 'a';
const b = 'b';
const c = 'c';
console.log(a, b, c);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
destructure | |
assign |
Test name | Executions per second |
---|---|
destructure | 869662.0 Ops/sec |
assign | 857703.0 Ops/sec |
Let's break down the JavaScript microbenchmark on MeasureThat.net.
What is tested?
The benchmark compares two approaches to log values in a JavaScript console:
const {a, b, c} = obj
syntax to extract properties from an object into separate variables.const a = 'a';
, const b = 'b';
, and const c = 'c';
to assign values to variables.Options compared
The benchmark compares the performance of these two approaches:
Library usage
There is no explicit library mentioned in the benchmark. However, some browsers may use libraries or polyfills under the hood to support advanced JavaScript features.
Special JS feature: Syntax sugar
The destructuring syntax (const {a, b, c} = obj
) is an example of "syntax sugar" – a concise way to express a concept that's already implemented in the language. This syntax is supported by modern browsers and can make code more readable and maintainable.
Other considerations
When choosing between these approaches, consider the following factors:
Alternatives
Other alternatives for similar use cases include:
Object.keys()
and looping over the array of keys to extract values from an object.Keep in mind that these alternatives might have different performance characteristics, code readability, and browser support requirements compared to the original approach tested by this benchmark.