mrkeck Sharing the Love

26Nov/120

Javascript: Faster Math.floor and parseInt

var str='123.55';
var num1=~~str; //num=123
var num2=Math.floor(str); //num=123
var num3=parseInt(str); //num=123

~~ operator gives the same result as Math.floor and parseInt but is slightly faster than Math.floor and is 6 times faster than parseInt.
Benchmark http://jsperf.com/rounding-numbers-down/15

Filed under: Optimization No Comments