indexOf vs RegExp

JavaScript performance comparison

Test case created by Joshua Inkenbrandt

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var items = [
      "Test Playlist",
      "Pizza is good",
      "The 90s called",
      "The 80s called",
      "Dummy playlist name here",
      "Test Playlist",
      "Pizza is good",
      "The 90s called",
      "The 80s called",
      "Dummy playlist name here",
      "Test Playlist",
      "Pizza is good",
      "The 90s called",
      "The 80s called",
      "Dummy playlist name here",
      "Test Playlist",
      "Pizza is good",
      "The 90s called",
      "The 80s called",
      "Dummy playlist name here",
      "Test Playlist",
      "Pizza is good",
      "The 90s called",
      "The 80s called",
      "Dummy playlist name here",
      "Test Playlist",
      "Pizza is good",
      "The 90s called",
      "The 80s called",
      "Dummy playlist name here",
      "Test Playlist",
      "Pizza is good",
      "The 90s called",
      "The 80s called",
      "Dummy playlist name here",
      "Test Playlist",
      "Pizza is good",
      "The 90s called",
      "The 80s called",
      "Dummy playlist name here",
      "Test Playlist",
      "Pizza is good",
      "The 90s called",
      "The 80s called",
      "Dummy playlist name here",
      "Test Playlist",
      "Pizza is good",
      "The 90s called",
      "The 80s called",
      "Dummy playlist name here",
      "Test Playlist",
      "Pizza is good",
      "The 90s called",
      "The 80s called",
      "Dummy playlist name here",
      "Test Playlist",
      "Pizza is good",
      "The 90s called",
      "The 80s called",
      "Dummy playlist name here",
      "Test Playlist",
      "Pizza is good",
      "The 90s called",
      "The 80s called",
      "Dummy playlist name here",
      "Test Playlist",
      "Pizza is good",
      "The 90s called",
      "The 80s called",
      "Dummy playlist name here",
    ];
   
    var queries = ["Test playlist", "The", "dum play"];
};
</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
indexOf
for (var i = 0; i < queries.length; i++) {

  var terms = queries[i].split(' ');

  for (var j = 0; j < items.length; j++) {
    for (var k = 0; k < terms.length; k++) {
      if (items[j].indexOf(terms[k]) != -1) {
        break;
      }
    }
  }
}
pending…
RegExp
for (var i = 0; i < queries.length; i++) {

  var terms = new RegExp(queries[i].replace(/\s+/g, '|'), 'gi');

  for (var j = 0; j < items.length; j++) {
    if (items[j].match(terms)) {
      break;
    }
  }
}
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