Conversions in Javascript
thetascript.com
Conversions in Javascript
1–10 of 14 posts
Re: Conversions in Javascript
#2Re: Conversions in Javascript
#3Re: Conversions in Javascript
#4parseInt('09') !== parseInt('9')
Re: Conversions in Javascript
#5Why people are so keen on micro-optimizing their code? The bottleneck lies on DOM, not on your conversions.
If you have a 10k csv file and want to make a graph using javascript, I believe it could come handy.
Re: Conversions in Javascript
#6* "All of the above does the same job...". Not true at all; the four examples do three very different things. Try them with a more interesting string argument, say '012.78':
parseInt('012.78') // 10 (number interpreted as octal integer)
Number('012.78') // 12.78
+'012.78' // 12.78
~~(1*'012.78') // 12
* "...and n1==n2==n3==n4" Using == instead of === here makes the statement meaningless. After all, this is also true: '12' == 12
* The "wicked" method, ~~( 1 * '12' ), smacks of cargo cult programming. No mention of the fact that it uses two operators instead of one? Why not test the two separately: 1*'012.78' // 12.78
~~'012.78' // 12
Complaints aside, the results for the "wicked" method certainly are interesting. Why is it (along with the two variations above) so extremely fast in Firefox?Here's an updated jsperf with those additional test cases:
Re: Conversions in Javascript
#7Performance doesn't help if your code is wrong.
Re: Conversions in Javascript
#8This article has a few problems. * "All of the above does the same job...". Not true at all; the four examples do three very different things. Try them with a more interesting string argument, say '012.78': parseInt('012.78') // 10 (number interpreted as octal integer) Number('012.78') // 12.78 +'012.78' // 12.78 ~~(1*'012.78') // 12 * "...and n1==n2==n3==n4" Using == instead of === here makes the statement meaningle…
No idea why +'12' is so slow, though - 0+'12' doesn't suffer from this...
Re: Conversions in Javascript
#9This article has a few problems. * "All of the above does the same job...". Not true at all; the four examples do three very different things. Try them with a more interesting string argument, say '012.78': parseInt('012.78') // 10 (number interpreted as octal integer) Number('012.78') // 12.78 +'012.78' // 12.78 ~~(1*'012.78') // 12 * "...and n1==n2==n3==n4" Using == instead of === here makes the statement meaningle…
http://jsperf.com/parsenumber/10
(scroll down the page to find any newer revisions)
Re: Conversions in Javascript
#10This article has a few problems. * "All of the above does the same job...". Not true at all; the four examples do three very different things. Try them with a more interesting string argument, say '012.78': parseInt('012.78') // 10 (number interpreted as octal integer) Number('012.78') // 12.78 +'012.78' // 12.78 ~~(1*'012.78') // 12 * "...and n1==n2==n3==n4" Using == instead of === here makes the statement meaningle…
Try this (creates a function and then show the source):
Function('return ~~(1 * "12");').toString();
And you get this result: "function anonymous() { return 12; }"
Notice that the expression has been converted to 12 in the function, so each call to it will just return 12 instead of converting it. That's why it is so much faster. The same is true for ">" and "1 * '12'"I created another test where it actually do the conversion, by assigning the string to a variable and use that in each test. There parseInt is faster in Firefox, Opera, Chrome and Safari. Surprisingly it is the slowest in IE, where +str is the fastest.
You can find it here: http://jsperf.com/parse-number-from-string
Also notice:
* All the logical operators (~, >, |) operates internally on a 32 bit signed integer, while parseInt, Number and + operates on a 64 bit signed float. Try to convert 2147483648 or 2333444555 for a little surprise.
* At the other jsperf someone added "0+'12'". That returns a string, not a number.