var to0 = {};
var to1 = {};
var wm = new WeakMap();
var sy = Symbol();
wm.set(to1, 1);
to1[sy] = 1;
window.r = 0
wm.set(to0, 1);
to0[sy] = 1;
r = wm.get(to1);
r = to1[sy];
r = Object.getOwnPropertyDescriptor(to1, sy).value;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
WeakMap set | |
Symbol Property set | |
WeakMap get | |
Symbol Property get | |
Symbol Property get (from own descriptor) |
Test name | Executions per second |
---|---|
WeakMap set | 14262492.0 Ops/sec |
Symbol Property set | 14462870.0 Ops/sec |
WeakMap get | 7349803.5 Ops/sec |
Symbol Property get | 7678899.0 Ops/sec |
Symbol Property get (from own descriptor) | 5070251.5 Ops/sec |
Let's break down the provided benchmark and explain what is being tested, the options compared, their pros and cons, and other considerations.
Benchmark Context
The benchmark compares the performance of setting and getting symbols in JavaScript using two approaches: WeakMap
with a symbol key and direct property access with a symbol. This comparison aims to evaluate which approach is faster for small-scale object lookup tasks.
Options Compared
WeakMap
to store an object with a symbol as its key.WeakMap
.Object.getOwnPropertyDescriptor
to retrieve the value of a property with a symbol as its name, using its own descriptor.Pros and Cons
Object.prototype
.Object.defineProperty
).Other Considerations
WeakMap
approach is likely to perform better due to its use of a hash table, which allows for fast lookups.Object.getOwnPropertyDescriptor
for symbol property retrieval may incur additional overhead compared to direct property access.Library/Functionality
WeakMap
, Symbol
, and Object.prototype
.Special JS Features/Syntax
In summary, this benchmark compares the performance of different approaches to setting and getting symbols in JavaScript. The results will help determine which approach is faster for small-scale object lookup tasks, considering factors like symbol creation overhead, hash table lookups, and property access mechanisms.