Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String() vs .toString() vs template string (optim)
(version: 0)
Comparing performance of:
String() vs .toString() vs template string
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
String()
let nums = new Array(100); for(let i = 0; i < 100; ++i) { nums.push(String(i)); }
.toString()
let nums = new Array(100); for(let i = 0; i < 100; ++i) { nums.push(i.toString()); }
template string
let nums = new Array(100); for(let i = 0; i < 100; ++i) { nums.push(`${i}`); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
String()
.toString()
template string
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Chrome OS 14541.0.0
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String()
252065.8 Ops/sec
.toString()
392377.6 Ops/sec
template string
548671.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **What is being tested?** The benchmark is comparing three different approaches to convert an integer to a string: 1. Using the `String()` function 2. Using the `.toString()` method 3. Using template literals (with the `${}` syntax) **Options comparison** The options are compared in terms of execution speed, which is measured by the number of executions per second. **Pros and cons of each approach:** 1. **`String()`**: This function creates a new string object using the `new String(i)` constructor. While it may seem like an older way to convert numbers to strings, it's still a valid method in modern JavaScript. However, creating a new string object can be slower than other methods. * Pros: Easy to understand and use, works for all browsers. * Cons: Can be slower due to the creation of a new string object. 2. **`.toString()`**: This method is built-in to the Number type in JavaScript and returns the string representation of the number. It's generally faster than creating a new string object. * Pros: Fast, uses built-in method, works for all browsers. * Cons: May not be as intuitive or easy to understand for some developers. 3. **Template literals**: Template literals are a relatively recent addition to JavaScript, introduced in ECMAScript 2015 (ES6). They allow you to embed expressions inside string literals using the `${}` syntax. * Pros: Fast, uses a built-in method, works for all browsers, and is often more readable than traditional string concatenation. * Cons: Requires support for ES6 features, may not be supported by older browsers. **Library usage** None of these approaches use any external libraries. They are all built-in or part of the JavaScript language. **Special JS feature or syntax** Template literals use a special syntax (`${}`) that is not commonly used in other contexts. However, it's widely accepted and used in modern web development. **Other alternatives** If you wanted to compare these approaches with alternative methods, here are some options: 1. Using `Number.toString()` instead of `.toString()` 2. Using a custom function to convert numbers to strings (e.g., using the `+` operator or the `Math.floor` method) 3. Using a library like Lodash's `toNumberString` function 4. Using a different language or platform-specific string conversion mechanism (if applicable) Keep in mind that some of these alternatives might not be as widely supported or used as the built-in methods mentioned earlier. In summary, the benchmark compares three approaches to convert integers to strings: `String()`, `.toString()`, and template literals. Each approach has its pros and cons, with template literals being a fast, modern, and often more readable option.
Related benchmarks:
string vs template vs toString
number to string: template literal vs toString vs string literal concat vs string constructor
String() primitive vs template literal interpolation for representing a number as a string
Concatenation vs Template String
number to string: template literal vs toString vs string literal concat vs string constructor(2)
Comments
Confirm delete:
Do you really want to delete benchmark?