Regular expression literals evaluation (v2)

Revision 2 of this benchmark created by Bjorn Tipling on


Description

MDN documentation claims that regular expressions literals are evaluated when a function is first evaluated, thus defining a literal in a loop should not affect performance.

Setup

var texts = [
      "foo bar foo bar foo bar",
      "foobarfoobarfoobar",
      "foo asdfasdfasdf bar"
    ]

Test runner

Ready to run.

Testing in
TestOps/sec
Using regular expression defined at beginning
var regexp = /foo bar/g;
texts.forEach(function (text) {
  text.match(regexp);
});
ready
Using regular expression defined in loop
texts.forEach(function (text) {
  text.match(/foo bar/g);
});
ready
Using a RegExp constructor
texts.forEach(function (text) {
  text.match(new RegExp("foo bar", "g"));
});
ready

Revisions

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

  • Revision 1: published by Bjorn Tipling on
  • Revision 2: published by Bjorn Tipling on