Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
parseInt vs Number // toString vs String
(version: 0)
Comparing performance of:
parseInt vs Number vs toString vs String
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var numbers = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789]; var strNumbers = ["1", "12", "123", "1234", "12345", "123456", "1234567", "12345678", "123456789"];
Tests:
parseInt
strNumbers.forEach(n => parseInt(n));
Number
strNumbers.forEach(n => Number(n));
toString
numbers.forEach(n => n.toString());
String
numbers.forEach(n => String(n));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
parseInt
Number
toString
String
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
parseInt
884295.3 Ops/sec
Number
939837.4 Ops/sec
toString
6382647.0 Ops/sec
String
947344.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark. **Benchmark Definition** The benchmark measures the performance of three different methods to convert numbers from strings: 1. `parseInt` (intended for parsing integers, but also works with strings) 2. `Number` (built-in JavaScript function to convert a string to a number) 3. `toString` (converting numbers to strings) The test creates two arrays: `numbers` and `strNumbers`, containing numeric values and their string representations, respectively. **Options Compared** The benchmark compares the performance of each method on both arrays: * `numbers.forEach(n => n.toString());` and `numbers.forEach(n => String(n));` * `strNumbers.forEach(n => parseInt(n));` and `strNumbers.forEach(n => Number(n));` These methods are compared in terms of execution speed, measured as "ExecutionsPerSecond". **Pros and Cons** Here's a brief summary of the pros and cons of each method: 1. **parseInt**: Pros: * Fastest execution ( highest ExecutionPerSecond) * Most JavaScript-like syntax * Can parse integers and strings with a radix parameter Cons: * May throw errors if parsing fails or if the radix is not provided * Not as straightforward to use as `Number` 2. **Number**: Pros: * Simple and intuitive syntax * Works well for most numeric values Cons: + Can be slower than `parseInt` (lower ExecutionPerSecond) 3. **toString** / **String**: Pros: + Fast execution (similar ExecutionPerSecond to `Number`) + Works well for converting numbers to strings Cons: + Requires a call to the method (`n.toString()` or `String(n)`) + Less JavaScript-like syntax compared to `parseInt` **Other Considerations** When choosing between these methods, consider the specific requirements of your use case: * If you need to parse integers with optional radix, `parseInt` is a good choice. * For simple conversions from strings to numbers, `Number` might be sufficient. * When converting numbers to strings, either `toString` or `String` can work well. **Library Usage** None of the benchmark methods rely on external libraries. However, if you're using modern JavaScript features (e.g., arrow functions, destructuring), those libraries might not be necessary for this specific use case. **Special JS Features** The benchmark doesn't explicitly test any special JavaScript features or syntax, but it does use: * Array methods (`forEach`) * Arrow functions * Property access syntax (`n.toString()`) These are standard JavaScript features and don't require a deep understanding of the language to comprehend.
Related benchmarks:
parseInt vs toString vs string literal vs + empty string
parseInt vs toString vs unary(+)
Implicit vs parseInt vs Number string to num
parseInt vs toString vs string literal vs + empty string vs String constructor
Comments
Confirm delete:
Do you really want to delete benchmark?