Flooring a number (v2)

Revision 2 of this benchmark created on


Description

Compare all the various ways to floor a number:

  • Math.floor(num) - tried and true
  • ~~num - double bitwise NOT
  • num|0 - bitwise OR with zero
  • num<<0 - left bitwise shift, but not really shifting
  • num>>0 - right bitwise shift, but not really shifting
  • num>>>0 - unsigned right bitwise shift, but not really shifting
  • num&0xFFFFFFFF - bitwise AND with all-bits-on
  • num^0^0 - double bitwise XOR with zero
  • num^0 - simple bitwise XOR with zero
  • num&num - bitwise AND with self

Setup

var num = 26.9;

Test runner

Ready to run.

Testing in
TestOps/sec
Math.floor
if (Math.floor(num) !== 26) throw "Error";
ready
Double bitwise NOT
if (~~num !== 26) throw "Error"; 
ready
Bitwise OR with 0
if ((num | 0) !== 26) throw "Error";
ready
Left shift with 0
if ((num << 0) !== 26) throw "Error";
ready
Right shift with 0
if ((num >> 0) !== 26) throw "Error";
ready
Unsigned right shift with 0
if ((num >>> 0) !== 26) throw "Error";
ready
Bitwise AND with all-bits-on
if ((num & 0xFFFFFFFF) !== 26) throw "Error";
ready
Double bitwise XOR with 0
if ((num ^ 0 ^ 0) !== 26) throw "Error";
ready
Simple bitwise XOR with 0
if ((num ^ 0) !== 26) throw "Error";
ready
Bitwise AND with self
if ((num & num) !== 26) throw "Error";
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.