1

I'm looking for a faster alternative of parseInt()


I recently found out this technique of converting a base 10 numeric string into an int in JavaScript:

var x = "1234567890" - 0 ;

Of course, this is restricted to base 10. I would like to know if there is any way of generalizing this to any base while preserving the efficiency. Thanks.

Somewhat relevant, but not directly related to the question: I ran a little test to compare its performance with the built-in parseInt() function. The -0 method is 15~16 times faster.


EDIT:

Here is the benchmarking code I'm using, and the console output is bellow it. Apparently, "1234"+0 is the fastest one.

var n = 1000000 ;
var x;

//parseInt with radix 10
t0 = Date.now(); console.log(t0);
for(i=0; i<n; i++) {
    x = parseInt("1234567890", 10) ;
}
t1 = Date.now(); console.log(t1);

console.log(t1-t0);


//parseInt without specifying the radix
t0 = Date.now(); console.log(t0);
for(i=0; i<n; i++) {
    x = parseInt("1234567890") ;
}
t1 = Date.now(); console.log(t1);

console.log(t1-t0);


//using - 0 to convert string to int
t0 = Date.now(); console.log(t0);
for(i=0; i<n; i++) {
    x = "1234567890" - 0 ;
}
t1 = Date.now(); console.log(t1);

console.log(t1-t0);

//using =+ to convert string to int
t0 = Date.now(); console.log(t0);
for(i=0; i<n; i++) {
    x = +"1234567890" ;
}
t1 = Date.now(); console.log(t1);

console.log(t1-t0);

Console:

[01:58:05.559] 1393225085558
[01:58:05.623] 1393225085622
[01:58:05.623] 64
[01:58:05.623] 1393225085623
[01:58:05.683] 1393225085683
[01:58:05.684] 60
[01:58:05.684] 1393225085684
[01:58:05.689] 1393225085689
[01:58:05.689] 5
[01:58:05.689] 1393225085689
[01:58:05.786] 1393225085786
[01:58:05.786] 97
Raiyan
  • 1,589
  • 1
  • 14
  • 28
  • 3
    `var x = +"1234567890";` is easier imho. – Ja͢ck Feb 24 '14 at 06:46
  • nice, can't wait to benchmark this. thanks @Jack – Raiyan Feb 24 '14 at 06:50
  • "Of course, this is restricted to base 10". Not sure if this is true. I tried "0xF"-0 and it returned 15 – SajithNair Feb 24 '14 at 06:51
  • @SajithNair The hexadecimal case is the only one that does that iirc – Ja͢ck Feb 24 '14 at 06:52
  • How do you expect to have a "general solution to any base" that doesn't use `parseInt()` where you pass the base? Confused as to what you could possibly be looking for? – jfriend00 Feb 24 '14 at 07:16
  • looking for a faster alternative of `parseInt()`. I did not say I don't want to pass the base. @jfriend00 – Raiyan Feb 24 '14 at 07:21
  • Well `parseInt()` is a generalized solution. If what you want is something faster than `parseInt()`, then please edit your question to explain that that is exactly what you want. Your question is simply not clear. Yes, you say that `parseInt()` isn't fast, but you don't say that performance is the issue you want to fix. You can see by Jack's answer that it must not be clear what you want. – jfriend00 Feb 24 '14 at 07:24
  • I edited before responding to your comment :) – Raiyan Feb 24 '14 at 07:25
  • I'd suggest you edit the title of your question too. – jfriend00 Feb 24 '14 at 07:26
  • What would you suggest changing the title to ? – Raiyan Feb 24 '14 at 07:27

1 Answers1

3

If you're looking for a generic solution, you will want to use parseInt():

var x = parseInt("1234567890"); // automatic base detection

var x = parseInt("777", 8); // force octal base (511)

Note that although the radix argument is optional, it's a good practice to always include it, i.e.:

parseInt("09"); // may fail
parseInt("09", 10); // always 9

If you're only looking for base-10 and base-16 conversions:

+"1234" // 1234
+"0xf" // 15
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • 2
    While [*parseInt*](http://ecma-international.org/ecma-262/5.1/#sec-15.1.2.2) is supposed to default to base 10 in ES5, most implementations still use ES ed 3 in this regard and `parseInt('09')` gives `0`, so always include a radix: `parseInt('09', 10)`. – RobG Feb 24 '14 at 06:50
  • @Jack, I'm actually looking for a faster (if possible) alternative of the `parseInt()`. See the the last two lines of my post, I mentiond `parseInt()`. – Raiyan Feb 24 '14 at 06:54
  • @Raiyan Decimal or hexadecimal are the only flavours that go faster :) – Ja͢ck Feb 24 '14 at 06:57
  • @Jack, I agree (until proven wrong) dec and hex are the only ones where this kind of type coercion works. But not sure if +"1234" is actually faster than "1234"+0. Please see the benchmarking code and console output I posted (EDIT). – Raiyan Feb 24 '14 at 07:23
  • @Raiyan I suggest you use [jsperf](http://jsperf.com) for benchmarks, don't make your own. – Ja͢ck Feb 24 '14 at 07:27