var Closure = (function initClosure() {
let value = 0;
return {
getValue: function getValue() {
return value;
},
setValue: function setValue(x) {
value = x;
}
};
}());
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 );
Closure.getValue();
Closure.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 | |
Closure - Getter function | |
Closure - Setter function |
Test name | Executions per second |
---|---|
Public - ES6 property (get) | 7462431.5 Ops/sec |
Public - Getter function | 6705466.0 Ops/sec |
Public - Directly get | 6156127.0 Ops/sec |
Public - ES6 property (set) | 65838800.0 Ops/sec |
Public - Setter function | 63478128.0 Ops/sec |
Public - Directly set | 64529084.0 Ops/sec |
Private - ES6 property (get) | 5265531.0 Ops/sec |
Private - Getter function | 5159066.5 Ops/sec |
Private - ES6 property (set) | 58952800.0 Ops/sec |
Private - Setter function | 58186636.0 Ops/sec |
Closure - Getter function | 54487944.0 Ops/sec |
Closure - Setter function | 50046764.0 Ops/sec |
Benchmark Overview
The provided benchmark measures the performance of different approaches to accessing and modifying properties in JavaScript classes.
Property Access Methods
There are three primary methods being tested:
obj.prop
).objProp
or this.prop
, to access and modify properties.setProp()
, to modify properties.Performance Comparison
The benchmark data shows the performance of each approach across various devices and browsers. The results indicate that:
Key Takeaways
Recommendations