Bitwise floor vs Math.floor

Benchmark created on


Description

Bitwise floor usually works faster than Math.floor, however it produces incorrect results when the 32nd bit integer is set. Bitwise operations also truncate the the values to 32 bits, but that is a different matter.

Obviously, the distribution of numbers will make some impact here. However, since the fast majority of numbers we deal with are small and do not use the 32nd bit, we only test a smaller number.

Setup

var val1 = 0x7fffffff;
    var val2 = val1 + 0.12345;
    var n;

Test runner

Ready to run.

Testing in
TestOps/sec
Just use floor
n = Math.floor(val1);
n = Math.floor(val2);
ready
Check and use bitwise
n = (val1 & 0x80000000) ? Math.floor(val1) : val1 | val1;
n = (val2 & 0x80000000) ? Math.floor(val2) : val2 | val2;
ready

Revisions

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