Creating a regex in and outside a loop

Benchmark created by Shane O'Sullivan on


Description

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

Test runner

Ready to run.

Testing in
TestOps/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/);
}
ready
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);
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Shane O'Sullivan on
  • Revision 3: published on