const a = Symbol();
const b = Symbol();
a === b
const a = 1;
const b = 2;
a === b
const a = '1';
const b = '2';
a === b
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Symbol | |
Number | |
String |
Test name | Executions per second |
---|---|
Symbol | 3666688.8 Ops/sec |
Number | 20849666.0 Ops/sec |
String | 19141878.0 Ops/sec |
Let's break down the benchmark and its results.
Benchmark Overview
The benchmark is designed to compare the performance of JavaScript primitives: numbers, strings, and symbols. The test cases check for equality between two variables, where one variable is initialized with a specific type (number, string, or symbol) and the other variable is initialized with a value that should not match the first variable.
Benchmark Test Cases
There are three test cases:
a === b
when both a
and b
are assigned the same symbol using const a = Symbol(); const b = Symbol();
.a === b
when both a
and b
are assigned numbers 1
and 2
, respectively.a === b
when both a
and b
are assigned strings '1'
and '2'
, respectively.Library and Special JS Features
In this benchmark, there is no explicit library used. However, the use of symbols (const a = Symbol(); const b = Symbol();
) introduces a special JavaScript feature: primitive values (numbers, strings, symbols) are distinct and can be compared using ===
or other comparison operators.
Performance Comparison
The results show that:
Pros and Cons
===
may return false positives for some numeric values (e.g., NaN).Other Alternatives
If this benchmark were not available, one could consider using other approaches:
performance.now()
function to measure execution times.Keep in mind that this is a simplified explanation, and there are many factors that can affect performance in real-world applications.