number-cast-vs-ternary

JavaScript performance comparison

Test case created by WebReflection

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  for (var a = [], i = 0; i < 1000; i++)
    a[i] = Math.random();
  
  var casted = [];
  
  function localTernary(arr, a) {
    for (var i = 0; i < a.length; i++)
      arr[i] = a[i] < .5 ? 1 : 0;
    return arr;
  }
  
  function globalNumber(arr, a) {
    for (var i = 0; i < a.length; i++)
      arr[i] = Number(a[i] < .5);
    return arr;
  }
  
  function proxiedNumber(arr, a) {
    for (var i = 0; i < a.length; i++)
      arr[i] = window.Number(a[i] < .5);
    return arr;
  }

};
</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
window.Number
casted = proxiedNumber([], a);
pending…
Number
casted = globalNumber([], a);
pending…
ternary
casted = localTernary([], a);
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