Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String() vs .toString() vs ``
(version: 0)
Comparing performance of:
String() vs .toString() vs String concatenation
Created:
2 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()); }
String concatenation
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()
String concatenation
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String()
151473.4 Ops/sec
.toString()
1779626.5 Ops/sec
String concatenation
3979090.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **What is being tested?** The provided JSON represents three individual test cases that compare different approaches to convert an integer value to a string in JavaScript: 1. Using the `String()` function 2. Using the `.toString()` method 3. Using template literals (string concatenation) Each test case creates an array `nums` and pushes a repeated number of values, either converted using one of these methods or a combination of them. **Options compared** The three options are compared in terms of their performance, measured by the number of executions per second (`ExecutionsPerSecond`) on a specific browser and device platform (Chrome 119 on Windows Desktop). **Pros and cons of each approach:** 1. **`String()` function**: * Pros: Simple, widely supported, and relatively fast. * Cons: May not be as efficient as other methods for large numbers or complex strings. 2. **`.toString()` method**: * Pros: Fast and efficient for most use cases. * Cons: Can produce unexpected results if used with non-numeric values (e.g., NaN, Infinity). 3. **Template literals (string concatenation)**: * Pros: Readable, efficient, and suitable for most string operations. * Cons: May be slower than `.toString()` for very large strings or in critical performance paths. **Library usage** None of the provided benchmark definitions explicitly use any external libraries. However, it's worth noting that template literals are a relatively modern feature introduced in ECMAScript 2015 (ES6). **Special JS features or syntax** No special JavaScript features or syntax are mentioned in the provided benchmark definitions. **Benchmark preparation code** The script preparation code for each test case is empty (`null`). This suggests that MeasureThat.net may be using a default setup or configuration for these tests, or they might be intentionally left blank to focus on the performance comparison between the three approaches. **Other alternatives** If you're interested in exploring alternative string conversion methods, here are a few examples: * Using `parseInt()` and then converting the result to a string: `nums.push(parseInt(num) + '');` * Using `Buffer` and encoding the number as a hexadecimal string: `const buffer = Buffer.alloc(8); buffer.writeUInt32BE(num); nums.push(buffer.toString('hex'));` * Using a third-party library like `lodash` for string manipulation: `nums.push(lodash.string.escape(null, num));` Keep in mind that these alternatives might not be as straightforward or efficient as the original three approaches and should only be considered for specific use cases or performance-critical code paths.
Related benchmarks:
String() vs toString
String() vs .toString() vs + string
number to string: template literal vs toString vs string literal concat vs string constructor
json stringify vs String() vs int tostring
'a string`.toString() vs `${'a string'}`
Comments
Confirm delete:
Do you really want to delete benchmark?