5 ms·
Here's another one for you, one of my favorites that used to be drastic: http://jsperf.com/pipetrunc http://jsperf.com/pipetrunc blah | 0; // fast! Mat
by simonsarris 12y ago
Here's another one for you, one of my favorites that used to be drastic: http://jsperf.com/pipetrunc http://jsperf.com/pipetrunc
blah | 0; // fast!
Math.floor(blah); // slow(er)! (except on FF nightly)
Caveat: Only works with numbers greater than -2147483649 and less than 2147483648.
- sheetjs 12y agoAnother caveat: for negative numbers, the bit-or rounds to zero while the floor rounds to negative infinity: > -0.5 -0.5 > (-0.5)|0 0 > Math.floor(-0.5) -1
- al2o3cr 12y agoA fine example why this sort of "who needs the specs, FULL SPEED AHEAD" hyper-optimization can get people into trouble.
- mraleph 12y agoCaveat: if you see numbers in the range of 1,500,000,000 that means your code was thrown away by the optimizing compiler. Microbenchmarks like that require a lot of care to measure something correctly. Check out my talk from LXJS2013 for more details: slides http://mrale.ph/talks/lxjs2013/ http://mrale.ph/talks/lxjs2013/ and video https://www.youtube.com/watch?v=65-RbBwZQdU https://www.youtube.com/watch?v=65-RbBwZQdU