mixing boolean and int

JavaScript performance comparison

Test case created by Paul Holden

Info

Mixing booleans and ints can result in deopts. In the second test case, I pass through true or false rather than 1 or 0, and subsequent calls to t() run more slowly.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = new Uint32Array(10000);
    function t(i, a) {
      arr[i] = a;
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
All ints
for(var i = 0; i < 100; ++i) {
  t(i, Math.random() < 0.5 ? 1 : 0);
}
t(100, Math.random() < 0.5 ? 1 : 0);
for(var i = 101; i < 10000; ++i) {
  t(i, Math.random() < 0.5 ? 1 : 0);
}
pending…
Ints and bools
for(var i = 0; i < 100; ++i) {
  t(i, Math.random() < 0.5 ? 1 : 0);
}
t(100, Math.random() < 0.5);  // NB passing true/false
for(var i = 101; i < 10000; ++i) {
  t(i, Math.random() < 0.5 ? 1 : 0);
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment