indexof or regex

JavaScript performance comparison

Test case created by ju1ius and last updated

Preparation code

<script>
var EXTS_DICT = { 'b9': 1, '9': 1, '#9': 1, '11': 1, '#11': 1, 'b13': 1, '13': 1 },
    EXTS_A = Object.keys(EXTS_DICT),
    EXTS_RX = /[b#]?(?:9|11|13)/,
    TESTS = [
      '#9', 'b13', '#2', 'b4', '11', '#11', '13', '14'
    ];

function testDict(value) {
  return EXTS_DICT.hasOwnProperty(value);
}
function testArray(value) {
  return -1 !== EXTS_A.indexOf(value);
}
function testRegex(value) {
  return EXTS_RX.test(value);
}

</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
Object.hasOwnProperty
TESTS.forEach(function(test){
  testDict(test);
});
pending…
Array.indexOf
TESTS.forEach(function(test){
  testArray(test);
});
pending…
RegExp.test
TESTS.forEach(function(test){
  testRegex(test);
});
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