Math.ceil(x) vs. ~~(x + 1 - Number.EPSILON) (v2)

Revision 2 of this benchmark created by Mark on


Description

It has been shown that using double bitwise NOT on a positive number is a faster way to achieve mathematical floor functionality than using Math.floor(). This is an attempt to discern whether similar performance gains can be had utilizing the same basic concept for mathematical ceiling functionality. (Note this will only work in browsers supporting ES6 Number.EPSILON).

Setup

var numbers = [],
        oneMinusEpsilon = 1 - Number.EPSILON;
    for(var i = 0; i < 100000; i++) numbers[i] = Math.random() * 100000;

Test runner

Ready to run.

Testing in
TestOps/sec
Math.ceil()
var test;
for(var i = 0; i < 100000; i++) test = Math.ceil(numbers[i]);
ready
Double bitwise NOT
var test;
for(var i = 0; i < 100000; i++) test = ~~(numbers[i] + oneMinusEpsilon);
ready
bitwise ZERO OR
var test;
for(var i = 0; i < 100000; i++) test = 0|numbers[i] + oneMinusEpsilon;
ready

Revisions

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