Best way to parse an int in Javascript
Asked Answered
N

3

15

One can convert a string to an integer in numerous ways, e.g.

  • parseInt("-1",10)
  • Math.floor("-1")
  • Number("-1")
  • "-1"|0
  • ~~"-1"

I assume the first is the canonical form, but e.g. uses the third one to coerce ints. There are probably more ways to do it.

What are the differences and benefits of using each of these? Which is expected to be the fastest?

Nammu answered 3/6, 2013 at 17:56 Comment(7)
Add the radix in parseInt, to be sure you get what you want in all browsers.Spider
jsperf - string to integerYerkovich
@Yerkovich This is a very valuable link! It seems eval("-1") is the slowest(by far), and the arithmetic hacks are the fastest. Wow. You could easily turn this into an interesting answerNammu
@elmes It's the slowest one. eval is always very slow.Spider
@elmes Interpreting that data is imo not that easy. Note all the browser differences, for example. The "hacks" seem to be specific to FF and IE, whereas Chrome and Opera favor parseInt().Yerkovich
@Yerkovich I've just run the test in Opera 12.10 and the arithmetics are the fastest, 3x the parseInt and 100x eval performance. I know this may depend, but some general trends are visible I thinkNammu
@elmes: The problem is, Opera's performance isn't representative of other browsers. You should be looking at Firefox, Chrome and IE.Siena
R
11

The canonical way to parse a decimal int is parseInt(str, 10).

Regarding other solutions :

  • parseInt("-1") : use it only if you like to live dangerously (some browsers assume "009" is decimal, not all)
  • Math.floor("-1") : it might be a floor and not an int, but that's not the right way if you want to be sure it's an integer
  • Number("-1") : maybe you want an object so you can call methods without promotion and you want to be sure there's no garbage (Number('3 flowers') ==> NaN)
  • "-1"|0, ~~"-1" and other combinations of implicit conversion and binary operation : you like code golf and don't want your code to be easily maintained (for those wondering : a binary operation takes the integer part of a number). As noted by Blender in comment, those solutions aren't suitable for big (positive or negative) numbers.

You should not use another solution than parseInt(str,10) if you don't need to ensure the string contains nothing else than an int. That's the fastest solution and, more importantly, the most readable. If a JS engine does some optimizations, there is no reason for other solutions to get faster than this one.

Richma answered 3/6, 2013 at 18:1 Comment(2)
Also, "n"|0 and ~~"n" fail for all numbers greater than 2^31 - 1 (and I think smaller than -2^31 - 1.Siena
the problem with parseInt and parseFloat is that they will stop on an invalid character and return a valid number if there were any valid digits. e.g., parseInt('1e9') === 1 and parseFloat('0x10') === 0. javascript is missing a "convert" (for lack of a better name) function that converts an entire string or fails.Voiture
B
3

What about unary plus? It looks like specially designed to type conversion.

+"-1" // -1
Bivalent answered 19/2, 2015 at 15:43 Comment(2)
This will also parse a non-integer numbers--the question was for integers. For example +"-1.2" will result in the number -1.2 vs parseInt("-1.2") which would correctly result in -1Anni
parseInt("-1.2") which would correctly result in -1 oh God. But how do I parse and fail if the string is not an integer?Monkeypot
A
-1

Be careful, because parseInt(0.000005) = 0, but parseInt(0.0000005) = 5. It is because input is converted into string, so 0.0000005 -> "5e-7". For me Math.trunc() works fine.

Asparagus answered 16/6 at 19:48 Comment(1)
OP asks about parsing a string. You should never pass a number to parseInt indeed. Were you looking for Math.round or Math.floor?Cowitch

© 2022 - 2024 — McMap. All rights reserved.