class Propertied {
constructor() {
this.key = 1;
}
}
window.Propertied = Propertied;
const symbol = Symbol("key");
class Symboled {
constructor() {
this[symbol] = 1;
}
}
window.Symboled = Symboled;
new Propertied();
new Symboled();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Property | |
Symbol |
Test name | Executions per second |
---|---|
Property | 99961976.0 Ops/sec |
Symbol | 94740024.0 Ops/sec |
I'll break down the benchmark and explain what's being tested, compared options, pros and cons of those approaches, and other considerations.
Benchmark Overview
The benchmark measures the performance difference between two ways to assign properties to an object in JavaScript:
this.key = 1;
)this[symbol] = 1;
)Options Compared
The benchmark compares the performance of these two approaches on the same JavaScript class constructor (Propertied
and Symboled
) with the following variations:
Propertied
Symboled
Pros and Cons of Each Approach
Other Considerations
window.Propertied
and window.Symboled
to make these classes globally accessible, allowing them to be instantiated directly in a browser console or test environment. This approach simplifies the benchmarking process but may not accurately reflect real-world scenarios where class definitions are typically wrapped in a module or used within a specific context.Library and Special JS Features
The benchmark uses the Symbol
function from the JavaScript Standard Library (ECMAScript 2015) to create symbols. The Symbol
function is used to define unique symbols that can be used as property keys.
Other Alternatives
If you're looking for alternative approaches or libraries, consider:
fast-symbol
which provides an optimized symbol creation mechanism.Keep in mind that these alternatives might not provide the same level of performance benefits as the original benchmark, which is specifically designed to compare the performance of property assignment and symbol assignment.