dongsubak
3/2/2019 - 1:21 AM

Quotient, reminder

Quotient, reminder
Javascript

I did some speed tests on Firefox.
-100/3 // -33.33..., 0.3663 millisec 
Math.floor(-100/3) // -34, 0.5016 millisec
 ~~(-100/3) // -33, 0.3619 millisec 
(-100/3>>0) // -33, 0.3632 millisec 
(-100/3|0) // -33, 0.3856 millisec 
(-100-(-100%3))/3 // -33, 0.3591 millisec 
/* a=-100, b=3 */ 
a/b // -33.33..., 0.4863 millisec
 Math.floor(a/b) // -34, 0.6019 millisec 
~~(a/b) // -33, 0.5148 millisec 
(a/b>>0) // -33, 0.5048 millisec 
(a/b|0) // -33, 0.5078 millisec 
(a-(a%b))/b // -33, 0.6649 millisec
The above is based on 10 million trials for each.
Conclusion: Use (a/b>>0) (or (~~(a/b)) or (a/b|0)) to achieve about 20% gain in efficiency. Also keep in mind that they are all inconsistent with Math.floor, when a/b<0 && a%b!=0.