Array Search vs. Bitmask Search

JavaScript performance comparison

Test case created by Adam Mark

Preparation code

<script>
  // Array setup
  var haystack1 = ["a", "b", "c", "d", "e"];
  var needles1 = ["a", "c", "e"];
  
  // Bitmask setup
  var opts = {
   "a": 1,
   "b": 2,
   "c": 4,
   "d": 8,
   "e": 16,
   "f": 32,
   "g": 64,
   "h": 128
  };
  var haystack2 = opts.a | opts.b | opts.c | opts.d | opts.e;
  var needles2 = opts.a | opts.c | opts.e;
</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
Array search
function contains(haystack, needles) {
 for (var i = 0; i < needles.length; i++) {
  if (haystack.indexOf(needles[i]) == -1) {
   return false;
  }
 }
 return true;
}

contains(haystack1, needles1);
pending…
Bitmask search
function contains(haystack, needles) {
 return (haystack & needles) >= needles;
}

contains(haystack2, needles2);
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.

0 Comments