var to0 = {};
var to1 = {};
var wm = new WeakMap();
var sy = Symbol();
wm.set(to1, 1);
to1[sy] = 1;
wm.set(to0, 1);
to0[sy] = 1;
let r = wm.get(to1);
let r = to1[sy];
let r = wm.has(to1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
WeakMap set | |
Symbol Property set | |
WeakMap get | |
Symbol Property get | |
WeakMap has |
Test name | Executions per second |
---|---|
WeakMap set | 2677249.5 Ops/sec |
Symbol Property set | 2861349.0 Ops/sec |
WeakMap get | 2725366.5 Ops/sec |
Symbol Property get | 2871802.5 Ops/sec |
WeakMap has | 2597251.8 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
Benchmark Definition
The provided JSON represents a benchmark that tests the performance of WeakMap
and Symbol Property
(also known as "symbol properties" or "property descriptors") in JavaScript.
What is tested?
The benchmark compares the performance of three different approaches:
Symbol
property to store values in an object. The Symbol
property is a unique identifier for a property that cannot be used as a regular property name.Options compared
The benchmark compares the performance of these three approaches:
Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Symbol
values (not strings or objects)Library
There is no specific library mentioned in the benchmark definition. The benchmark only uses built-in JavaScript features: WeakMap
, Symbol
properties, and basic object literals.
Special JS feature or syntax
The benchmark uses the following special features:
Other alternatives
If you were to write this benchmark from scratch, you might consider using other approaches, such as:
Object.hasOwn()
or Map.prototype.has()
)Keep in mind that these alternatives might not be as straightforward to implement and may require more expertise in JavaScript performance optimization.