Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String to number, parseInt, +, or * 1
(version: 0)
Which method of casting a string to a number is fastest
Comparing performance of:
parseInt vs +stringVar vs stringVar * 1
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var n = [ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", ];
Tests:
parseInt
let i = 0; while (i < 10000) { i++; n.map((s) => parseInt(s)); }
+stringVar
let i = 0; while (i < 10000) { i++; n.map((s) => +s); }
stringVar * 1
let i = 0; while (i < 10000) { i++; n.map((s) => s * 1); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
parseInt
+stringVar
stringVar * 1
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:130.0) Gecko/20100101 Firefox/130.0
Browser/OS:
Firefox 130 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
parseInt
67.1 Ops/sec
+stringVar
73.1 Ops/sec
stringVar * 1
80.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and the pros and cons of each approach. **Benchmark Overview** The benchmark measures the performance of three different methods for casting a string to a number in JavaScript: 1. `parseInt`: Converts a string to an integer using the `parseInt()` function. 2. `+stringVar`: Performs implicit type conversion by adding 0 to the string variable (essentially converting it to a number). 3. `stringVar * 1`: Multiplies the string variable by 1, which also converts it to a number. **Comparison** The benchmark compares the performance of these three methods on a large dataset (`n`) containing strings from "0" to "100". The test cases are identical except for the method being tested. **Pros and Cons** Here's a brief overview of each method: 1. `parseInt`: * Pros: Well-established, widely supported, and relatively fast. * Cons: May not work as expected if the input string is not in a numeric format. 2. `+stringVar`: * Pros: Simple and easy to understand, works with most numeric strings. * Cons: May be slower than `parseInt` due to the overhead of implicit type conversion. 3. `stringVar * 1`: * Pros: Fast and efficient, as it leverages the language's arithmetic operations. * Cons: Less intuitive for developers, may not work with non-numeric strings. **Library/Functionality Used** In this benchmark, no specific library or functionality is used beyond the built-in JavaScript functions `parseInt()` and implicit type conversion (`+stringVar`). **Special JS Feature/Syntax** There's no special JavaScript feature or syntax being tested in this benchmark. The focus is solely on the performance comparison of these three methods. **Other Alternatives** If you're looking for alternative ways to convert strings to numbers, consider: 1. Using a library like ` numeral.js` which provides more advanced numeric formatting and parsing capabilities. 2. Implementing your own string-to-number conversion function using regular expressions or other techniques. 3. Leveraging modern JavaScript features like template literals (`${expression}`) or the `Number()` function. Keep in mind that these alternatives may not provide significant performance benefits over the original methods being compared, but they can offer additional functionality and flexibility for specific use cases. I hope this explanation helps!
Related benchmarks:
parseInt vs toString vs string literal vs + empty string
parseInt vs toString vs unary(+)
parseInt vs Number // toString vs String
parseInt vs toString vs string literal vs + empty string vs String constructor
Comments
Confirm delete:
Do you really want to delete benchmark?