Creating a regex in and outside a loop

JavaScript performance comparison

Test case created by Shane O'Sullivan

Info

Tests the cost of repeatedly declaring a regular expression rather than just declaring it once

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
In a loop
var txt1 = 'mary is at home';
var txt2 = 'john is at home';
for(var i = 0; i < 100; i++) {
  txt1.match(/mary/);
  txt2.match(/mary/);
}
pending…
Outside a loop
var txt1 = 'mary is at home';
var txt2 = 'john is at home';
var regex = /mary/;
for(var i = 0; i < 100; i++) {
  txt1.match(regex);
  txt2.match(regex);
}
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