Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
int to string convertation
(version: 0)
Comparing performance of:
String vs toString(10) vs toString vs interpolation vs plus
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
String
const arr = Array(1000).map(() => ~~(Math.random() * 1000)) const b = arr.map((num) => String(num))
toString(10)
const arr = Array(1000).map(() => ~~(Math.random() * 1000)) const b = arr.map((num) => num.toString(10))
toString
const arr = Array(1000).map(() => ~~(Math.random() * 1000)) const b = arr.map((num) => num.toString())
interpolation
const arr = Array(1000).map(() => ~~(Math.random() * 1000)) const b = arr.map((num) => `${num}`)
plus
const arr = Array(1000).map(() => ~~(Math.random() * 1000)) const b = arr.map((num) => '' + num)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
String
toString(10)
toString
interpolation
plus
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Ubuntu
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String
131526.0 Ops/sec
toString(10)
132893.3 Ops/sec
toString
135607.5 Ops/sec
interpolation
145555.4 Ops/sec
plus
133422.9 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 benchmark definition json represents a simple JavaScript code snippet that creates an array of 1000 random integers between 0 and 1000, maps each integer to its string representation using different methods (e.g., `toString()`, `toString(10)`, interpolation with template literals `${num}`, and concatenation with the `+` operator), and stores the results in a new array. **Options Compared** The benchmark compares four different approaches to convert integers to strings: 1. **String**: Uses the `String()` function. 2. **toString()**: Uses the `toString()` method on the number object (e.g., `num.toString()`). 3. **toString(10)**: Uses the `toString(10)` method on the number object with a radix of 10. 4. **interpolation**: Uses template literals `${num}` to convert the integer to a string. 5. **plus**: Uses the `+` operator to concatenate the integer with an empty string. **Pros and Cons** Here's a brief overview of each approach: 1. **String()**: Simple and widely supported, but might be slower due to the overhead of calling a function. 2. **toString()**: Fast and efficient, as it's a native method on the number object. However, it might not work correctly for negative numbers or non-integer values. 3. **toString(10)**: Similar to `toString()`, but with a radix of 10, which can affect the output format. This approach is specific to JavaScript and might not be widely supported elsewhere. 4. **interpolation**: Fast and efficient, as it uses template literals, which are optimized for string interpolation. However, it requires modern JavaScript features (ES6+). 5. **plus**: Simple and fast, but may lead to performance issues with large numbers due to the overhead of concatenation. **Library Usage** None of the benchmark test cases use external libraries. **Special JS Features or Syntax** The `interpolation` approach uses template literals, which is a modern JavaScript feature introduced in ES6. This means that the benchmark results might not be compatible with older browsers or environments that don't support this syntax. **Other Alternatives** If you're looking for alternative approaches to convert integers to strings, you could consider: 1. **parseInt()` and `concat()`: Use `parseInt(num)` followed by concatenation with an empty string using the `+` operator. 2. **Number()` and `toString()`: Convert the integer to a number using `Number(num)` and then call `toString()` on it. 3. **Buffer.toString()`: In Node.js environments, you can use the `Buffer` class's `toString()` method to convert integers to strings. Keep in mind that these alternatives might have varying degrees of performance, compatibility, and readability depending on your specific use case.
Related benchmarks:
Number Conversion Speed
Intl.NumberFormat vs toLocaleString vs Custom Formatter vs Pre-created Intl formatter
eweewqe3
Intl.NumberFormat vs toLocalString vs Intl.NumberFormat instantiated
Compare String to Number conversion
Comments
Confirm delete:
Do you really want to delete benchmark?