Philly Zipcode

JavaScript performance comparison

Test case created by Jeffrey Mealo

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var isPhillyZipcode12 = (function() {
   
      var re1 = /^191\d{2}/; // Matches: 191##
      var re2 = /^9[0|5|8|9]|8[0|6|9]|7[4|6]|5[6-9]|6[3-9]|00/; //matches the holes in sparse 191xx zip code ranges
      var re3 = /^192(55|44)/;
   
      return function(zip) {
        zip = zip + "";
        return (re1.test(zip) && !re2.test(zip.substr(3, 2))) || re3.test(zip);
      }
    })();
   
    var isPhillyZipcode11 = (function() {
   
      var re1 = /191[0-9][0-9]/; // Matches: 191##
      var re2 = /9[0|5|8|9]|8[0|6|9]|7[4|6]|5[6-9]|6[3-9]|00/; //matches the holes in sparse 191xx zip code ranges
      var re3 = /192(55|44)/;
   
      return function(zip) {
        zip = zip + "";
        return (re1.test(zip) && !re2.test(zip.substr(3, 2))) || re3.test(zip);
      }
    })();
   
    var isPhillyZipcode10 = (function() {
   
      var re1 = /191\d{2}/; // Matches: 191##
      var re2 = /9[0|5|8|9]|8[0|6|9]|7[4|6]|5[6-9]|6[3-9]|00/; //matches the holes in sparse 191xx zip code ranges
      var re3 = /192(55|44)/;
   
      return function(zip) {
        zip = zip + "";
        return (re1.test(zip) && !re2.test(zip.substr(3, 2))) || re3.test(zip);
      }
    })();
   
    var isPhillyZipcode9 = (function() {
   
      var re1 = /191\d{2}/; // Matches: 191##
      var re2 = /9[0|5|8|9]|8[0|6|9]|7[4|6]|5[6-9]|6[3-9]|00/; //matches the holes in sparse 191xx zip code ranges
      var re3 = /192(55|44)/;
   
      return function(zip) {
        zip = (zip + "").trim().substr(0, 5); //cast as string, trims whitespace, get first 5 characters
        return (re1.test(zip) && !re2.test(zip.substr(3, 2))) || re3.test(zip);
      }
    })();
   
    var isPhillyZipcode2 = (function() {
   
      var re1 = /^191/;
      var re2 = /9[0|5|8|9]|8[0|6|9]|7[4|6]|5[6-9]|6[3-9]|00/;
   
      return function(zipcode) {
        zipcode = zipcode + ""; //cast as string
        if (re1.test(zipcode)) {
          return !(re2.test(zipcode.substr(3, 2)));
        } else {
          //only two philly zipcodes start with 192xx
          return (zipcode === '19244' || zipcode === '19255');
        }
      }
    })();
   
    var isPhillyZipcode3 = (function() {
   
      var re = /9[0|5|8|9]|8[0|6|9]|7[4|6]|5[6-9]|6[3-9]|00/;
   
      return function(zipcode) {
        zipcode = zipcode + ""; //cast as string
        if (zipcode.substr(0, 3) === '191') {
          return !(re.test(zipcode.substr(3, 2)));
        } else {
          //only two philly zipcodes start with 192xx
          return (zipcode === '19244' || zipcode === '19255');
        }
      }
    })();
   
    function isPhillyZipcode1(zipcode) {
      zipcode = "" + zipcode;
      zipcode = parseInt(zipcode);
   
      var valid_entries = [19101, 19102, 19103, 19104, 19105, 19106, 19107, 19108, 19112, 19113, 19114, 19115, 19116, 19117, 19118, 19119, 19120, 19121, 19122, 19123, 19124, 19125, 19126, 19127, 19128, 19129, 19130, 19131, 19132, 19133, 19134, 19135, 19136, 19137, 19138, 19139, 19140, 19141, 19142, 19143, 19144, 19145, 19146, 19147, 19148, 19149, 19150, 19151, 19152, 19153, 19154, 19155, 19160, 19161, 19162, 19170, 19171, 19172, 19173, 19175, 19177, 19178, 19179, 19181, 19182, 19183, 19184, 19185, 19187, 19188, 19191, 19192, 19193, 19194, 19196, 19197, 19244, 19255, 19109, 19110, 19111];
   
      var inPhilly = (zipcode && valid_entries.indexOf(zipcode) != -1);
   
      return inPhilly;
    }
   
    function isPhillyZipcode4(zipcode) {
   
      var re = /9[0|5|8|9]|8[0|6|9]|7[4|6]|5[6-9]|6[3-9]|00/;
   
      zipcode = zipcode + ""; //cast as string
      if (zipcode.substr(0, 3) === '191') {
        return !(re.test(zipcode.substr(3, 2)));
      } else {
        //only two philly zipcodes start with 192xx
        return (zipcode === '19244' || zipcode === '19255');
      }
    }
   
    function isPhillyZipcode5(zip) {
   
      var re = /9[0|5|8|9]|8[0|6|9]|7[4|6]|5[6-9]|6[3-9]|00/;
   
      zip = zip.toString(); //cast as string
      return (zip.substr(0, 3) === '191') && re.test(zip.substr(3, 2)) || (zip === '19244') || (zip === '19255');
    }
   
    var isPhillyZipcode6 = (function() {
   
      var re1 = /^191/;
      var re2 = /9[0|5|8|9]|8[0|6|9]|7[4|6]|5[6-9]|6[3-9]|00/;
   
      return function(zip) {
        zip = zip + ""; //cast as string
        return (re1.test(zip) && (!re2.test(zip.substr(3, 2)))) || (zip === '19244') || (zip === '19255');
      }
    })();
   
    var isPhillyZipcode7 = (function() {
   
      var re1 = /^191/;
      var re2 = /1(9[0|5|8|9]|8[0|6|9]|7[4|6]|5[6-9]|6[3-9]|00)/;
      var re3 = /192(55|44)/;
   
      return function(zip) {
        zip = zip + ""; //cast as string
        return (re1.test(zip) && (!re2.test(zip.substr(3, 2)) || re3.test(zip)));
      }
    })();
   
    var isPhillyZipcode8 = (function() {
   
      var re1 = /^191/;
      var re2 = /1(9[0|5|8|9]|8[0|6|9]|7[4|6]|5[6-9]|6[3-9]|00)/;
   
      return function(zip) {
        zip = zip + ""; //cast as string
        return (re1.test(zip) && (!re2.test(zip.substr(3, 2)) || (zip === '19244') || (zip === '19255')));
      }
    })();
   
    var zipcodes = [19101, 19102, 19103, 19104, 19105, 19106, 19107, 19108, 19112, 19113, 19114, 19115, 19116, 19117, 19118, 19119, 19120, 19121, 19122, 19123, 19124, 19125, 19126, 19127, 19128, 19129, 19130, 19131, 19132, 19133, 19134, 19135, 19136, 19137, 19138, 19139, 19140, 19141, 19142, 19143, 19144, 19145, 19146, 19147, 19148, 19149, 19150, 19151, 19152, 19153, 19154, 19155, 19160, 19161, 19162, 19170, 19171, 19172, 19173, 19175, 19177, 19178, 19179, 19181, 19182, 19183, 19184, 19185, 19187, 19188, 19191, 19192, 19193, 19194, 19196, 19197, 19244, 19255, 19109, 19110, 19111];
};
</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
isPhillyZipcode1
for (var x = 0, len = 99999; x < len; x++) {
  isPhillyZipcode1(x);
}
pending…
isPhillyZipcode2
for (var x = 0, len = 99999; x < len; x++) {
  isPhillyZipcode2(x);
}
pending…
isPhillyZipcode3
for (var x = 0, len = 99999; x < len; x++) {
  isPhillyZipcode3(x);
}
pending…
isPhillyZipcode4
for (var x = 0, len = 99999; x < len; x++) {
  isPhillyZipcode4(x);
}
pending…
isPhillyZipcode5
for (var x = 0, len = 99999; x < len; x++) {
  isPhillyZipcode5(x);
}
pending…
isPhillyZipcode6
for (var x = 0, len = 99999; x < len; x++) {
  isPhillyZipcode6(x);
}
pending…
isPhillyZipcode7
for (var x = 0, len = 99999; x < len; x++) {
  isPhillyZipcode7(x);
}
pending…
isPhillyZipcode8
for (var x = 0, len = 99999; x < len; x++) {
  isPhillyZipcode8(x);
}
pending…
isPhillyZipcode9
for (var x = 0, len = 99999; x < len; x++) {
  isPhillyZipcode9(x);
}
pending…
isPhillyZipcode10
for (var x = 0, len = 99999; x < len; x++) {
  isPhillyZipcode10(x);
}
pending…
isPhillyZipcode11
for (var x = 0, len = 99999; x < len; x++) {
  isPhillyZipcode11(x);
}
pending…
isPhillyZipcode12
for (var x = 0, len = 99999; x < len; x++) {
  isPhillyZipcode12(x);
}
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

Add a comment