Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String() vs .toString() vs `${num}`
(version: 0)
Comparing performance of:
String() vs .toString() vs `${num}`
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
String()
let num = 500; let nums = []; for(let i = 0; i < 100; ++i) { nums.push(String(num)); }
.toString()
let num = 500; let nums = []; for(let i = 0; i < 100; ++i) { nums.push(num.toString()); }
`${num}`
let num = 500; let nums = []; for(let i = 0; i < 100; ++i) { nums.push(`${num}`); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
String()
.toString()
`${num}`
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String()
828992.1 Ops/sec
.toString()
1505921.9 Ops/sec
`${num}`
5815055.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is defined in JSON format, which represents a set of three test cases for comparing the performance of different ways to convert an integer value `num` into a string. **Test Cases** There are three test cases: 1. **String()**: This test case uses the `String()` function (also known as the "string primitive" or " primitive conversion") to convert `num` into a string. 2. **.toString()**: This test case uses the `toString()` method on the `num` variable, which is also used for converting an integer value to a string. 3. **`${num}`**: This test case uses template literals (`${...}`) with the `num` value inside. Template literals are a feature introduced in ECMAScript 2015 (ES6). **Library and Purpose** * None of these test cases rely on any external libraries. **Special JS Features or Syntax** * The use of template literals (`${...}`) is the only special syntax being tested. Now, let's discuss the pros and cons of each approach: * **String()**: This is a simple and widely supported way to convert an integer value into a string. It's likely to be fast because it doesn't require any extra function calls or object lookups. However, it may not be as efficient if the resulting string needs to be manipulated further (e.g., concatenation, formatting). * **.toString()**: This method is also widely supported and can be used for converting an integer value into a string. It's likely to be faster than `String()` because it doesn't require a separate function call or object lookup. However, it may not work as expected if the resulting string needs to be manipulated further (e.g., concatenation, formatting). * **`${num}`**: Template literals are a powerful feature in JavaScript that can simplify string interpolation. They're likely to be faster than `String()` and `.toString()`, especially for simple cases like this one, because they avoid the overhead of creating a new string object. However, they may not work as expected if the resulting string needs to be manipulated further (e.g., concatenation, formatting). **Other Considerations** * The benchmark is run on Chrome 106 on a Windows Desktop. * The number of executions per second is reported for each test case. **Alternatives** There are no obvious alternatives being tested in this benchmark. However, if you wanted to compare the performance of these three approaches with other string conversion methods, such as: * Using `parseInt()` and then concatenating strings * Using `String.fromCharCode()` and then concatenating strings * Using a regex pattern and the `replace()` method Keep in mind that these alternatives would likely have different performance characteristics than the ones being tested here. I hope this explanation helps!
Related benchmarks:
Intl.NumberFormat vs toLocalString
toFixed() vs Math.round().toString()
String to int vs int to string 2
toFixed() vs String(Math.floor()
Intl.NumberFormat vs toLocalString yoyo
Comments
Confirm delete:
Do you really want to delete benchmark?