16

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?

7
  • 1
    Add the radix in parseInt, to be sure you get what you want in all browsers. Commented Jun 3, 2013 at 17:58
  • 4
    jsperf - string to integer Commented Jun 3, 2013 at 18:00
  • @Sirko 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 answer Commented Jun 3, 2013 at 18:06
  • @elmes It's the slowest one. eval is always very slow. Commented Jun 3, 2013 at 18:08
  • 1
    @elmes: The problem is, Opera's performance isn't representative of other browsers. You should be looking at Firefox, Chrome and IE. Commented Jun 3, 2013 at 19:01

3 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Also, "n"|0 and ~~"n" fail for all numbers greater than 2^31 - 1 (and I think smaller than -2^31 - 1.
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.
Note: "canonical" in this answer simply stands for "best practice" here. If anyone was wondering like me, whether that's really what it implies.
3

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

+"-1" // -1

2 Comments

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 -1
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?
-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.

1 Comment

OP asks about parsing a string. You should never pass a number to parseInt indeed. Were you looking for Math.round or Math.floor?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.