Truncating a number to an integer

~~, bitwise << and Math.trunc. Math.floor will always round down to the nearest integer, which means that for negative numbers it's going to be one less than all the other ones. You shouldn't use the others for rounding down for the same reason - the answer will be wrong for negative numbers.
4 years ago
User agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36
Test name Executions per second
Bitwise 4694159.5 Ops/sec
~~ 4699844.0 Ops/sec
Math.trunc 2651746.2 Ops/sec
Script Preparation code:
Tests:
  • Bitwise

  • ~~

  • Math.trunc

Open this result on MeasureThat.net