Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string vs number vs symbol
(version: 0)
Comparing performance of:
Symbol vs Number vs String
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Symbol
const a = Symbol(); const b = Symbol(); a === b
Number
const a = 1; const b = 2; a === b
String
const a = '1'; const b = '2'; a === b
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Symbol
Number
String
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Symbol
33009098.0 Ops/sec
Number
179178176.0 Ops/sec
String
189619152.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. **Symbol**: Checks if `a === b` when both `a` and `b` are assigned the same symbol using `const a = Symbol(); const b = Symbol();`. 2. **Number**: Checks if `a === b` when both `a` and `b` are assigned numbers `1` and `2`, respectively. 3. **String**: Checks if `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: * **Symbol**: Symbols have the highest execution rate, with an average of 6652672 executions per second. * **Number**: Numbers have a lower execution rate than symbols but higher than strings. The average execution rate is around 171858736 executions per second. * **String**: Strings have the lowest execution rate, with an average of 171858736 executions per second. **Pros and Cons** * **Symbols**: + Pros: High performance due to their primitive nature and immutability. + Cons: Limited use cases, as they are not commonly used in JavaScript programming. * **Numbers**: + Pros: Fast execution rates, widely used in numerical computations. + Cons: Comparison operators like `===` may return false positives for some numeric values (e.g., NaN). * **Strings**: + Pros: Common use case, easy to work with, and has many built-in methods. + Cons: Lower performance compared to numbers and symbols. **Other Alternatives** If this benchmark were not available, one could consider using other approaches: 1. **Use existing benchmarking libraries**: e.g., Benchmark.js or Microbenchmark. 2. **Implement custom benchmarking loops**: using JavaScript's built-in `performance.now()` function to measure execution times. 3. **Compare performance on different platforms**: to account for differences in browser behavior. Keep in mind that this is a simplified explanation, and there are many factors that can affect performance in real-world applications.
Related benchmarks:
+string vs Number vs parseInt
String() primitive vs template literal interpolation for representing a number as a string
parseInt vs Number string to number
Number + String vs String + String
Number constructor with short argument vs long argument
Comments
Confirm delete:
Do you really want to delete benchmark?