Int as boolean array (via bitmasks)

JavaScript performance comparison

Revision 3 of this test case created by picocreator

Info

The usage of a single integer, as a boolean array (up to 31 booleans).

For more information, refer to : http://blog.pic-o.com/2013/01/int-as-array-bitmap-bitmask-think-boo-array-in-js-but-not-limited-to/

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var boolArr = new Array(31);
    var boolInt = 0;
    var bitMaskArr = new Array(31);
   
    var _a = 0,
        _b, _c, _d;
   
    for (_a = 0; _a < 31; ++_a) {
      boolArr[_a] = false;
      bitMaskArr[_a] = Math.pow(2, _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
boolArr set 0 index true
boolArr[0] = true;
pending…
boolInt set 0 index true
boolInt |= 0x1;
pending…
boolArr set all true
for (_a = 0; _a < 30; ++_a) {
  boolArr[_a] = true;
}
pending…
boolInt set all true (Math.pow)
for (_a = 0; _a < 30; ++_a) {
  boolInt |= Math.pow(2, _a);
}
pending…
boolInt set all true (parseInt)
_b = "";
for (_a = 0; _a < 8; ++_a) {
  //0x
  boolInt |= parseInt("1" + _b, 16);
  boolInt |= parseInt("2" + _b, 16);

  if (_a < 7) {
    boolInt |= parseInt("4" + _b, 16);
    boolInt |= parseInt("8" + _b, 16);
  }
  _b += "0";

}
pending…
boolInt set all true (direct multiplication)
_b = 1;
for (_a = 0; _a < 30; ++_a) {
  boolInt |= _b;
  _b = _b * 2;
}
pending…
boolInt set all true (bitmask array)
for (_a = 0; _a < 30; ++_a) {
  boolInt |= bitMaskArr[_a];
}
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