views:

1183

answers:

9

Firstly - my description ;)

I've got a XmlHttpRequests JSON response from the server. MySQL driver outputs all data as string and PHP returns it as it is, so any integer is returned as string, therefore:

Is there any fast alternative (hack) for parseInt() function in JS which can parse pure numeric string, e.g.

var foo = {"bar": "123"};
...
foo.bar = parseInt(foo.bar); // (int) 123
+8  A: 

First off, have you actually documented that it's slow and is causing problems? Otherwise, I wouldn't bother looking for a solution, because there really isn't a problem.

Secondly, I would guess that since parseInt is a native JS-method, it would be implemented in a way that is very fast, and probably in the native language of the VM (probably C, depending on the browser/VM). I think you could have some trouble making a faster method out of pure JS. =)

Of course, I'm not a JS guru, so I don't know for sure, but this is what my intuition tells me, and tends to be the standard answer to "how would I make a faster alternative for libraryFunction()?" questions.

Daniel Bruce
There are other reasons for not using parseInt -- compare parseInt("07") to parseInt("08")
Adam Lassek
+2  A: 

The Number constructor also exists, but it should be the same as parseInt in term of speed (as already said you should correct the PHP part instead of the javascript one anyway) :

var i = "123";
i = new Number(i); // Number numeric wrapper
var j = "123";
j = Number(j); // Number primitive

BTW if someone is interested i searched by curiosity for the V8 (Google chrome) implementation of parseInt and it's here on google code.

VirtualBlackFox
drop the `new`, use `i = Number(i)` instead to get a primitive instead of an object
Christoph
Exact, i will add it. Thanks.
VirtualBlackFox
+6  A: 

Cast it to an int in PHP before you json_encode() it:

$foo->bar = (int)$foo->bar;
print('var foo = ' . json_encode($foo));

Incidentally, when using parseInt it's good practice to always specify the second parameter unless you really want string starting with 0 to be interpreted as octal and so on:

parseInt('010', 10); // 10
Greg
It's complicated, I don't have access to Php server.
shfx
+2  A: 

You aren't going to get better than parseInt, but the real bug is that the PHP is providing what is supposed to be a number as a string.

And ditto to what Daniel said - don't go looking for micro-optimisations like this until you have benchmarked your code and discovered that it's worth doing.

A: 

if the objects are larger you could try JSON, it is a typed format so you do not need to convert the values.

Hippo
A: 

How slow can it be? How many times per second is this process being called? How many different numeric return values are there? I whipped together a script and tested 100,000 numbers. Parsing them from strings took 687ms. Searching them in an array took 541ms. That's a very small improvement. I agree with other posters. You may not get better than the native parseInt() method.

souLTower
+3  A: 

Type casting in JavaScript is done via the constructor functions of the built-in types without new, ie

foo.bar = Number(foo.bar);

This differs from parseInt() in serveral ways:

  • leading zeros won't trigger octal mode
  • floating point values will be parsed as well
  • the whole string is parsed, ie if it contains additional non-numeric characters, the return value will be NaN
Christoph
A: 

Casting is a wee bit faster than parsing but slower than searching.

Also, in Firefox the fastest method turns out to be parseInt() followed by searching. Firefox also turned out to be 6 times faster on average than IE. Interesting.

Cool idea using the unary operator. In Firefox that turned out to be comparable to parseInt(). In IE it turned out to be the fastest method.

souLTower
+4  A: 

To convert to an integer simply use the unary + operator, it should be the fastest way:

var int = +string;

Conversions to other types can be done in a similar manner:

var string = otherType + "";
var bool = !!anything;

More info.

Aistina
This is clever idea, thanks! I like dusoft's multiply too
shfx