IF AND vs Obj Lookup

JavaScript performance comparison

Revision 2 of this test case created

Info

Test multiple IF AND conditions against an object lookup

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var account_number = '1110000200';
    var account_numbers = ['1110000136', '1110000138', '1110000127', '1110000139', '1110000140', '1110000167'];
    var account_numbers_obj = {
      '1110000136': 1,
      '1110000138': 1,
      '1110000127': 1,
      '1110000139': 1,
      '1110000140': 1,
      '1110000167': 1
    };
    if (!Array.prototype.some) {
      Array.prototype.some = function(fun /*, thisp */) {
        'use strict';
   
        if (this == null) {
          throw new TypeError();
        }
   
        var thisp, i,
            t = Object(this),
            len = t.length >>> 0;
        if (typeof fun !== 'function') {
          throw new TypeError();
        }
   
        thisp = arguments[1];
        for (i = 0; i < len; i++) {
          if (i in t && fun.call(thisp, t[i], i, t)) {
            return true;
          }
        }
   
        return false;
      };
    }
};
</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
IF AND (array)
if (account_number != account_numbers[0] && account_number != account_numbers[1] && account_number != account_numbers[2] && account_number != account_numbers[3] && account_number != account_numbers[4] && account_number != account_numbers[5]) {
  return true;
}
pending…
IF AND (no array)
if (account_number != '1110000136' && account_number != '1110000138' && account_number != '1110000127' && account_number != '1110000139' && account_number != '1110000140' && account_number != '1110000167') {
  return true;
}
pending…
OBJ
if (account_numbers_obj[account_number]) {
  return true;
}
pending…
SWITCH
switch (account_number) {
  case '1110000136':
    return true;
    break;
  case '1110000138':
    return true;
    break;
  case '1110000127':
    return true;
    break;
  case '1110000139':
    return true;
    break;
  case '1110000140':
    return true;
    break;
  case '1110000167':
    return true;
    break;
  default:
    return false;
}
pending…
ARRAY SOME
account_numbers.some(function (e) {
    if (e === account_number) {
        return true;
    }
});
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