class Test1 {
constructor(name) {
this.name = name;
}
}
class Test2 {
constructor(name) {
this._name = name;
}
get name() {
return this._name;
}
}
class Test3 {
constructor(name) {
Object.defineProperties(this, {
name: {
enumerable : true,
configurable: false,
get() {
return name;
}
}
});
}
}
class Test4 {
#name;
constructor(name) {
this.#name = name;
}
get name() {
return this.#name;
}
}
class Test5 extends Test4 {
}
const a20 = new Array(20).fill(undefined);
function test1() {
const a = new Test1('test');
return a20.map(() => a.name === 'test');
}
function test2() {
const a = new Test2('test');
return a20.map(() => a.name === 'test');
}
function test3() {
const a = new Test3('test');
return a20.map(() => a.name === 'test');
}
function test4() {
const a = new Test4('test');
return a20.map(() => a.name === 'test');
}
function test5() {
const a = new Test5('test');
return a20.map(() => a.name === 'test');
}
test1();
test2();
test3()
test4();
test5();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
test1 | |
test2 | |
test3 | |
test4 | |
test5 (by extends) |
Test name | Executions per second |
---|---|
test1 | 9353361.0 Ops/sec |
test2 | 9555166.0 Ops/sec |
test3 | 830130.4 Ops/sec |
test4 | 9582554.0 Ops/sec |
test5 (by extends) | 7384350.0 Ops/sec |
Measuring JavaScript performance is a complex task, and MeasureThat.net provides a great platform for testing various scenarios.
Benchmark Definition
The provided JSON represents a benchmark that tests the execution time of different test cases in a JavaScript class with private properties and read-only properties. The benchmark defines five test cases:
test1
: Creates an instance of Test1
and checks if the name
property is equal to 'test'
.test2
: Creates an instance of Test2
and checks if the name
property is equal to 'test'
.test3
: Uses Object.defineProperties
to define a read-only property name
on Test3
. Then, it creates an instance of Test3
and checks if the name
property is equal to 'test'
.test4
: Creates an instance of Test4
with a private property _name
using the #
symbol (also known as "private fields" in JavaScript). The test case then checks if the name
property is equal to 'test'
.test5
: Extends Test4
and creates an instance of Test5
. It then checks if the name
property is equal to 'test'
.Options Compared
The benchmark compares different approaches for accessing private properties:
Test1
uses a public name
property.Test2
uses a getter function for its name
property, which is also public.test3
uses Object.defineProperties
to define a read-only property name
.test4
uses the #
symbol (private fields) to create a private _name
property.test5
extends Test4
and inherits its private properties.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
name
property: Easy to access, but may not be intended for external use (e.g., due to security concerns). Pros: easy to implement, well-supported in most browsers.name
property: Explicitly controls access to the private property. Pros: provides fine-grained control over accessibility; cons: can lead to boilerplate code.Object.defineProperties
: Allows for more granular control over property behavior. Pros: flexible, but requires understanding of property descriptors. Cons: may be less familiar to developers.#
symbol): Introduced in ECMAScript 2020, provides a concise way to declare private properties. Pros: concise and expressive; cons: relatively new feature, may not be supported in older browsers or environments.extends
: Allows for code reuse by inheriting private properties from a parent class. Pros: promotes modularity, efficient code reuse; cons: may require additional setup (e.g., using classes).Other Considerations
When choosing an approach, consider the following:
Object.defineProperties
or private fields.#
symbol.Alternative Implementations
If you want to test other aspects of JavaScript performance, MeasureThat.net offers various benchmarks for:
Function
constructors or eval
)These additional benchmarks can help you identify performance bottlenecks in different areas of your JavaScript codebase.