3 ms·
They should also add: Math.floor(someNumber) // 2.6 becomes 2 someNumber | 0 // 2.6 becomes 2 Not only because it saves characters, but becaus
by simonsarris 15y ago
They should also add:
Math.floor(someNumber) // 2.6 becomes 2
someNumber | 0 // 2.6 becomes 2
Not only because it saves characters, but because it might actually be used in the real world (Whereas most of the ones in the article should really be left to a minifier)
- jedschmidt 15y agoThat's already covered, along with use of ~~ for the same purpose.
- simonsarris 15y agoOops, sorry. I must have misspelled 'floor' on my CTRL-f
- afshin 15y agoThe problem with this technique is that it will not work for large numbers (in my build of node, for example, the largest number it will work with is 2^31 - 1: (Math.pow(2, 31) - 0.1) // base number 2147483647.9 ~~(Math.pow(2, 31) - 0.1) // works! 2147483647 (Math.pow(2, 31) + 0.1) // base number 2147483648.1 ~~(Math.pow(2, 31) + 0.1) // doesn't work! -2147483648
- deleted 15y ago[deleted]