Javascript compiled regex

JavaScript performance comparison

Revision 16 of this test case created by Albert Rossinski

Info

Add .compile() documentation!

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var normal = /\(\d+,\d+,\d+,\d+\)/;
    var object = new RegExp('\\(\\d+,\\d+,\\d+,\\d+\\)');
    var compiled1 = new RegExp('\\(\\d+,\\d+,\\d+,\\d+\\)');
    compiled1.compile();
    var compiled2 = new RegExp()
    compiled2.compile('\\(\\d+,\\d+,\\d+,\\d+\\)');
    var compiled3 = /\(\d+,\d+,\d+,\d+\)/;
    compiled3.compile();
    var globalNormal = /\(\d+,\d+,\d+,\d+\)/g;
   
    var someText = "(1234,12451,32352,43456)\n(858,9564,454,76467)\n(17183,4482,3558,345)\n(343477,344,38955,669)\n(890434,347845,555,885769)\n(5664,334,767,34447)\n(45,4574,455647,44477)\n(1234,12451,32352,43456)\n(858,9564,454,76467)\n(17183,4482,3558,345)\n(343477,344,38955,669)\n(890434,347845,555,885769)\n(5664,334,767,34447)\n(45,4574,455647,44477)\n(1234,12451,32352,43456)\n(858,9564,454,76467)\n(17183,4482,3558,345)\n(343477,344,38955,669)\n(890434,347845,555,885769)\n(5664,334,767,34447)\n(45,4574,455647,44477)\n(1234,12451,32352,43456)\n(858,9564,454,76467)\n(17183,4482,3558,345)\n(343477,344,38955,669)\n(890434,347845,555,885769)\n(5664,334,767,34447)\n(45,4574,455647,44477)\n(1234,12451,32352,43456)\n(858,9564,454,76467)\n(17183,4482,3558,345)\n(343477,344,38955,669)\n(890434,347845,555,885769)\n(5664,334,767,34447)\n(45,4574,455647,44477)\n(1234,12451,32352,43456)\n(858,9564,454,76467)\n(17183,4482,3558,345)\n(343477,344,38955,669)\n(890434,347845,555,885769)\n(5664,334,767,34447)\n(45,4574,455647,44477)";
    var someTextArray = someText.split('\n');
};
</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
normal RegExp
for (i in someTextArray) {
  normal.exec(someTextArray[i]);
}
pending…
object RegExp
for (i in someTextArray) {
  object.exec(someTextArray[i]);
}
pending…
compiled1 RegExp
for (i in someTextArray) {
  compiled1.exec(someTextArray[i]);
}
pending…
compiled2 RegExp
  for (i in someTextArray) {
    compiled2.exec(someTextArray[i]);
  }
pending…
compiled3 RegExp literal
for (i in someTextArray) {
  compiled3.exec(someTextArray[i]);
}
pending…
global normal RegExp
  globalNormal.exec(''); // to reset the regex
  while (match = globalNormal.exec(someText)) {
    var a = match;
  }
pending…
inline RegExp
  for (i in someTextArray) {
    /\(\d+,\d+,\d+,\d+\)/.exec(someTextArray[i]);
  }
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