Integer division: Math.floor(a/b) vs ~~(a/b)

Benchmark created by Jonas Finnemann Jensen on


Description

Efficient integer division [1] suggest to either use Math.floor(a / b) or ~~ (a / b). Here we test which is fastest...

[1] http://stackoverflow.com/questions/4228356/integer-division-in-javascript

Setup

Data = [];
    var i = 0;
    for(i = 0; i < 1000; i++){
    Data.push(i + 7);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Math.floor(a/b)
var i;
var result = [];
for(i = 0; i < 1000; i++){
result.push(Math.floor(Data[i] / 7));
}
ready
~~(a/b)
var i;
var result = [];
for(i = 0; i < 1000; i++){
result.push(~~(Data[i] / 7));
}
ready
((a - a % b) / b)
var i;
var result = [];
for(i = 0; i < 1000; i++){
var a = Data[i];
result.push(((a - a % 7) / 7));
}
ready

Revisions

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

  • Revision 1: published by Jonas Finnemann Jensen on
  • Revision 2: published on
  • Revision 3: published by Jonas Finnemann Jensen on
  • Revision 4: published by walking-tree-vs-flattened-tree on