Javascript compiled regex

JavaScript performance comparison

Revision 10 of this test case created

Info

Just what exactly does .compile() do?

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var normal = /\(\d+,\d+,\d+,\d+\)/;
    var compiled1 = new RegExp('\\(\\d+,\\d+,\\d+,\\d+\\)');
    compiled1.compile();
    var compiled2 = new RegExp('\\(\\d+,\\d+,\\d+,\\d+\\)');
    var globalNormal = /\(\d+,\d+,\d+,\d+\)/g;
    var globalCompiled = new RegExp('\\(\\d+,\\d+,\\d+,\\d+\\)', 'g');
    globalCompile.compile();
    var globalCompiled2 = new RegExp('\\(\\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
Test each line with a normal RegExp
for (i in someTextArray) {
   normal.exec(someTextArray[i]);
}
pending…
Test each line with a compiled RegExp
for (i in someTextArray) {
   compiled1.exec(someTextArray[i]);
}
pending…
Test each line with a constructor on an incorrect regex
for (i in someTextArray) {
   compiled2.exec(someTextArray[i]);
}
pending…
Parse the whole string at once, loop over the matches
globalNormal.exec(''); // to reset the regex
while (match = globalNormal.exec(someText)) {
    var a = match;
}
pending…
Parse the whole string at once, loop over the matches, compiled
globalCompiled.exec(''); // to reset the regex
while (match = globalCompiled.exec(someText)) {
    var a = match;
}
pending…
Parse the whole string at once, loop over the matches, compiled2
globalCompiled2.exec(''); // to reset the regex
while (match = globalCompiled2.exec(someText)) {
    var a = match;
}
pending…
Test each line with a bare 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