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

Revision 3 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 - 1E-16;
    for(var i = 0; i < 100000; i++) numbers[i] = Math.random() * 100000 - 50000;

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] + ( ( numbers[i] < 0 ) ? 0 : oneMinusEpsilon ) );
ready

Revisions

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