RegExp test / search / match vs. indexOf

JavaScript performance comparison

Revision 78 of this test case created by

Preparation code

<script>
  var str = "But I must explain to you how all this mistaken idea of denouncing of a pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure";
  var match = 'again';
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var precompiledRegex = /(again)/g;

};
</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
test
/(inline|ruby)/.test(str);
pending…
search
str.search(/(inline|ruby)/) > -1;
pending…
match
str.match(/(inline|ruby)/).length > 0;
pending…
indexOf
str.indexOf(/(inline|ruby)/) > -1;
pending…
precompiled test
precompiledRegex.test(str);
pending…
precompiled search
str.search(precompiledRegex) > -1;
pending…
precompiled match
str.match(precompiledRegex).length > 0;
pending…
indexOf string primitive
str.indexOf(match) !== -1
pending…
Primitive comparison
str === "inline" || str === "inline-block" || str === "inline-table" || str === "ruby";
pending…
Substring
str.substring(0,match.length) === match
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