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;
r = Object.hasOwn(sy) ? to1[sy] : undefined;
--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) | |
Symbol Property get (if hasOwn) |
Test name | Executions per second |
---|---|
WeakMap set | 12726717.0 Ops/sec |
Symbol Property set | 13497805.0 Ops/sec |
WeakMap get | 6409639.0 Ops/sec |
Symbol Property get | 6529689.0 Ops/sec |
Symbol Property get (from own descriptor) | 4280086.0 Ops/sec |
Symbol Property get (if hasOwn) | 3817115.0 Ops/sec |
I'll break down the provided benchmark and its test cases, explaining what's being tested, the pros and cons of each approach, and other considerations.
Benchmark Definition JSON
The benchmark definition is a simple JavaScript script that creates two objects (to0
and to1
) and sets up a WeakMap (wm
) with a Symbol property (sy
). The script then tests various operations on these objects using the WeakMap.
Test Cases
There are six test cases:
wm.set(to0, 1);
size
property correctly.to0[sy] = 1;
r = wm.get(to1);
r = to1[sy];
r = Object.getOwnPropertyDescriptor(to1, sy).value;
Object.getOwnPropertyDescriptor
to access a property's descriptor and then retrieves its value using the value
property of the descriptor.r = Object.hasOwn(sy) ? to1[sy] : undefined;
Object.hasOwn
and then accessing its value using the Symbol as a key.Options Compared
The benchmark compares two approaches:
Pros and Cons of Each Approach
Other Considerations
Object.getOwnPropertyDescriptor
).Alternatives
Other alternatives for testing memory management, symbol-based access, or similar scenarios might include:
Keep in mind that these alternatives might not directly address the specific use cases tested by the provided benchmark but can provide a broader understanding of performance and efficiency in JavaScript development.