var numbers = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789];
var strNumbers = ["1", "12", "123", "1234", "12345", "123456", "1234567", "12345678", "123456789"];
numbers.forEach(n => parseInt(n, 10));
strNumbers.forEach(n => parseInt(n, 10));
numbers.forEach(n => n.toString());
strNumbers.forEach(n => n.toString());
numbers.forEach(n => `${n}`);
strNumbers.forEach(n => `${n}`);
numbers.forEach(n => n + "");
strNumbers.forEach(n => n + "");
numbers.forEach(n => String(n));
strNumbers.forEach(n => String(n));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
parseInt a number | |
parseInt a string | |
toString a number | |
toString a string | |
string literal a number | |
string literal a string | |
add empty string to number | |
add empty string to string | |
String constructor on a number | |
String constructor on a string |
Test name | Executions per second |
---|---|
parseInt a number | 109923440.0 Ops/sec |
parseInt a string | 11348308.0 Ops/sec |
toString a number | 26995610.0 Ops/sec |
toString a string | 25914776.0 Ops/sec |
string literal a number | 109509896.0 Ops/sec |
string literal a string | 45735916.0 Ops/sec |
add empty string to number | 109673256.0 Ops/sec |
add empty string to string | 95952936.0 Ops/sec |
String constructor on a number | 20568506.0 Ops/sec |
String constructor on a string | 32321870.0 Ops/sec |
Let's break down the benchmark and explain what is being tested.
Benchmark Overview
The benchmark measures the performance of various JavaScript operations on numbers and strings, specifically:
parseInt()
toString()
or template literals (${n}
)String()
constructorOptions Being Compared
There are four main options being compared:
A) parseInt(n, 10)
(converting a string to an integer)
B) n.toString()
(converting a number to a string)
C) ${n}
(template literal with a number as its value)
D) n + ""
(adding an empty string to a number)
Pros and Cons of Each Approach
A) parseInt(n, 10)
:
Pros: Fast conversion from string to integer Cons: Can be slower than other approaches for large numbers or strings
B) n.toString()
:
Pros: Fast conversion from number to string Cons: May not work as expected for very large numbers due to string size limits
C) ${n}
(template literal):
Pros: Fast and efficient way to create a string from a number Cons: Requires modern JavaScript engines that support template literals
D) n + ""
:
Pros: Simple and straightforward way to add an empty string to a number Cons: May be slower than other approaches due to the addition operation
Library Usage
There is no explicit library usage in this benchmark, but it's worth noting that some of these operations may rely on underlying libraries or engines.
Special JS Features/Syntax
The template literal syntax (${n}
) is a modern JavaScript feature introduced in ECMAScript 2015. It allows embedding expressions inside string literals. The String()
constructor and the addition operation (n + ""
) are standard JavaScript operators.
Other Alternatives
Some alternative approaches could be:
parse-int
or string-parser
to convert numbers to integersfunction toString(n) { return n.toString(); }
${n} + ""
However, these alternatives are not being tested in this benchmark.