const obj = {a: { value: true, d: { hello: true } }, b: true, c: 'hello' };
const a = obj.a;
const b = obj.b;
const c = obj.c;
const d = a.d;
console.log(a);
console.log(b);
console.log(c);
console.log(d);
const obj = {a: { value: true, d: { hello: true } }, b: true, c: 'hello' };
const {a, b, c} = obj;
const { d } = a;
console.log(a);
console.log(b);
console.log(c);
console.log(d);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Assign | |
Destructure |
Test name | Executions per second |
---|---|
Assign | 55649.1 Ops/sec |
Destructure | 62782.1 Ops/sec |
Measuring the performance of JavaScript assignments and destructuring can be an interesting exercise.
Benchmark Definition
The benchmark defines two different ways to access properties from an object: assignment and destructuring. The first test case, "Assign", assigns the value
property of the inner object to a variable using the dot notation (const d = a.d;
). The second test case, "Destructure", uses destructuring to extract multiple values directly from the object (const { d } = a;
).
Options Compared
The benchmark compares two approaches:
const d = a.d;
)const { d } = a;
)Pros and Cons
Assignment:
Pros:
Cons:
Destructuring:
Pros:
Cons:
Library and Purpose
In both test cases, the obj
variable is an object with nested properties. The a
and b
variables are assigned to properties of the outer object using the dot notation (const a = obj.a;
, const b = obj.b;
). There are no external libraries used in this benchmark.
Special JS Feature or Syntax
There is no special JavaScript feature or syntax used in this benchmark. The tests focus on comparing two common ways to access properties from an object.
Other Alternatives
If you're interested in exploring alternative approaches, here are a few options:
{ ...obj }
) to create a new object with extracted properties.Object.keys()
and forEach()
to iterate over the object's properties and extract values.Keep in mind that each of these alternatives has its own trade-offs and performance characteristics, which may not be as well-suited for this specific benchmark.