Math ceiling inline

JavaScript performance comparison

Test case created by Alexander O'Mara

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  //Technically wrong to alias this way, but just for testing.
  var mathCeil = Math.ceil;
  
  function myCeil(f) {
      return f + ((1 - (f % 1)) % 1);
  }
  
  function myCeilBranch(f) {
      var r = f % 1;
      return r ? f + (1 - r) : f;
  }
  

};

Benchmark.prototype.teardown = function() {
  void(dummy);

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Math.ceil
var dummy = null;
for (var i = 10; i++ < 1000;) {
    var f = i / 100;
    dummy = Math.ceil(f);
}
pending…
mathCeil (Math.ceil alias)
var dummy = null;
for (var i = 10; i++ < 1000;) {
    var f = i / 100;
    dummy = mathCeil(f);
}
pending…
myCeil (custom ceil)
var dummy = null;
for (var i = 10; i++ < 1000;) {
    var f = i / 100;
    dummy = myCeil(f);
}
pending…
inline myCeil (custom inlined)
var dummy = null;
for (var i = 10; i++ < 1000;) {
    var f = i / 100;
    dummy = f + ((1 - (f % 1)) % 1);
}
pending…
myCeilBranch (custom ceil with branching)
var dummy = null;
for (var i = 10; i++ < 1000;) {
    var f = i / 100;
    dummy = myCeilBranch(f);
}
pending…
inline myCeilBranch (custom inlined)
var dummy = null;
for (var i = 10; i++ < 1000;) {
    var f = i / 100;
    var r = f % 1;
    dummy = r ? f + (1 - r) : f;
}
pending…

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

Compare results of other browsers

0 Comments