var globalData = {
data: {
data1: {
data2: {
property: "value",
}
}
},
};
var {data: {data1: {data2: {property}} }} = globalData;
var property = globalData.data.data1.data2.property;
var property = globalData['data']['data1']['data2']['property']
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Destructuring Notation | |
Dot Notation | |
Bracket Notation |
Test name | Executions per second |
---|---|
Destructuring Notation | 8400939.0 Ops/sec |
Dot Notation | 7696638.5 Ops/sec |
Bracket Notation | 7214762.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and analyze the provided benchmark definition.
Benchmark Definition
The benchmark measures the performance difference between three different ways to access an object property in JavaScript:
{data: {data1: {data2: {property}} }} = globalData;
)var property = globalData.data.data1.data2.property;
)var property = globalData['data']['data1']['data2']['property'];
)Options Compared
The benchmark compares the performance of these three approaches:
.
to access nested properties)[ ]
to access nested properties)Pros and Cons
Here's a brief analysis of each approach:
Library and Syntax Considerations
The benchmark doesn't use any external libraries, so it only focuses on JavaScript syntax.
There are no special JS features or syntax mentioned in the benchmark definition.
Other Alternatives
If you're interested in exploring other approaches, here are a few alternatives:
Object.keys()
and forEach()
to iterate over properties instead of dot notation.JSON.parse()
to parse a JSON string containing nested properties (not tested in this benchmark).Keep in mind that these alternatives might not be relevant to the specific use case of this benchmark.
Benchmark Preparation Code
The script preparation code sets up an object globalData
with a nested structure:
var globalData = {
data: {
data1: {
data2: {
property: "value"
}
}
}
};
This object is used to test the performance of accessing its properties using different notation.
Individual Test Cases
Each test case consists of a single benchmark definition with a different notation:
var {data: {data1: {data2: {property}} }} = globalData;
var property = globalData.data.data1.data2.property;
var property = globalData['data']['data1']['data2']['property'];
These test cases are run multiple times to collect performance data.
Latest Benchmark Result
The benchmark result shows the execution rate per second for each notation on a specific browser (Firefox 118) on a desktop device:
ExecutionsPerSecond
= 82780928.0ExecutionsPerSecond
= 676816704.0ExecutionsPerSecond
= 743621824.0These results indicate that destructuring notation is the fastest, followed by bracket notation, and then dot notation.