Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String() vs .toString() vs + vs ES6
(version: 1)
Comparing performance of:
String() vs .toString() vs Plus string vs String plus vs ES6
Created:
3 years ago
by:
Registered User
Jump to the latest result
Tests:
String()
let num = 500; let nums = []; for (let i = 0; i < 1000; i++) { nums.push(String(num)); }
.toString()
let num = 500; let nums = []; for (let i = 0; i < 1000; i++) { nums.push(num.toString()); }
Plus string
let num = 500; let nums = []; for (let i = 0; i < 1000; i++) { nums.push(num + ''); }
String plus
let num = 500; let nums = []; for (let i = 0; i < 1000; i++) { nums.push('' + num); }
ES6
let num = 500; let nums = []; for (let i = 0; i < 1000; i++) { nums.push(`${num}`); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
String()
.toString()
Plus string
String plus
ES6
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years 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()
10686.8 Ops/sec
.toString()
129030.5 Ops/sec
Plus string
336617.2 Ops/sec
String plus
320945.5 Ops/sec
ES6
357746.6 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. **Benchmark Definition** The provided JSON defines a benchmark with four test cases: 1. **`"String()"`**: This test case creates an empty array `nums` and pushes the string representation of a number 500 to it 1000 times. 2. **`".toString()"`**: Similar to the first test case, but uses the `.toString()` method to convert the number to a string instead of the built-in `String()` function. 3. **`"Plus string"`**: This test case uses the unary plus operator (`+`) to convert the number 500 to a string and then pushes it to the array `nums`. 4. **`"ES6"`**: This test case uses template literals (introduced in ECMAScript 2015) to create a string from the number 500 and pushes it to the array `nums`. **Comparison of Options** Let's analyze each option: * **`String()` vs `.toString()`**: Both methods convert a value to its string representation. The main difference is that `.toString()` is a method call, while `String()` is a function invocation (more on this later). In terms of performance, both are usually equivalent. * **`+` vs `'' + num`**: Using the unary plus operator (`+`) versus concatenating an empty string (`''`) with the number using template literals. The `+` approach is generally faster because it avoids creating a temporary string object and instead uses a simple arithmetic operation. * **ES6 (template literals)**: Template literals are a feature introduced in ECMAScript 2015, which allows you to embed expressions inside string literals using backticks (`). This syntax can make code more readable, but performance-wise, it's usually comparable to the `+` approach. **Library and Special JS Features** * **`String()` vs `.toString()`**: These two methods are built-in JavaScript functions. `.toString()` is a method call, while `String()` is a function invocation. * **ES6 (template literals)**: Template literals are a standard feature in modern JavaScript engines. They're not specific to any library. **Other Considerations** When comparing these options, keep in mind that the performance difference might be negligible for most use cases. However, if you need to optimize string conversion in a critical path of your code, understanding these differences can help you make informed decisions. **Alternatives** If you're looking for alternatives, here are a few: * For string concatenation, consider using the `Array.prototype.join()` method or a library like Lodash, which provides efficient string joining functions. * If you need to convert numbers to strings frequently, you might want to consider pre-converting your number data type to strings (e.g., by adding a `toString()` method on your object) or using a different data structure that inherently supports string representation. I hope this explanation helps you understand the world of JavaScript microbenchmarks and provides valuable insights into optimizing string conversion in your code!
Related benchmarks:
String() vs toString
String() vs .toString() vs + string
String to int vs int to string 2
json stringify vs String() vs int tostring
string.at(-1) vs string[string.length-1]
Comments
Confirm delete:
Do you really want to delete benchmark?