class Private {
#value = 0;
constructor() {
this.#value = 1
}
get value() {
return this.#value
}
set value(x) {
this.#value = x
}
getValue() {
return this.#value
}
setValue(x) {
this.#value = x
}
}
class Public {
value = 0;
constructor() {
this.value = 1
}
get value() {
return this.value
}
set value(x) {
this.value = x
}
getValue() {
return this.value
}
setValue(x) {
this.value = x
}
}
var private = new Private();
var public = new Public();
var val = 0;
val = public.value;
val = public.getValue();
val = public.value;
public.value = 1;
public.setValue(1);
public.value = 1;
val = private.value;
val = private.getValue();
private.value = 1;
private.setValue(1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Public ES6 property (get) | |
Public Getter function | |
Public Directly get | |
Public ES6 property (set) | |
Public Setter function | |
Public Directly set | |
Private ES6 property (get) | |
Private Getter function | |
Private ES6 property (set) | |
Private Setter function |
Test name | Executions per second |
---|---|
Public ES6 property (get) | 299593344.0 Ops/sec |
Public Getter function | 282615040.0 Ops/sec |
Public Directly get | 282472256.0 Ops/sec |
Public ES6 property (set) | 430841952.0 Ops/sec |
Public Setter function | 418993696.0 Ops/sec |
Public Directly set | 425608800.0 Ops/sec |
Private ES6 property (get) | 286338688.0 Ops/sec |
Private Getter function | 287016160.0 Ops/sec |
Private ES6 property (set) | 376575200.0 Ops/sec |
Private Setter function | 385027200.0 Ops/sec |
Let's dive into the benchmark and explain what is being tested, compared, and their pros and cons.
Benchmark Definition:
The benchmark measures the performance difference between accessing object attributes directly (using dot notation) versus using ES6 property access with getters and setters. The test cases compare these approaches for both public and private properties of an object.
Test Cases:
There are 8 test cases in total, each comparing a different approach:
Approaches Compared:
The benchmark compares two approaches:
obj.prop
)..
operator to access properties, which invokes a getter or setter function depending on the context.Pros and Cons of Each Approach:
Libraries Used:
The benchmark uses JavaScript, which is a dynamic language that allows for easy creation of getters and setters using functions. The Object.defineProperty()
method is used to define getter and setter properties on objects.
Device and Browser:
The benchmark results are reported for Firefox Mobile 125 on an Android device. This means the results may not be representative of other devices or browsers.
Now, let's take a deep breath and move on to analyzing the results...