RegEx vs regex literal (v2)

Revision 2 of this benchmark created by Rowaa[SR13] on


Preparation HTML

<script>
  var test_str = " a aa  b bbb b bb       b",
  flags = 'g',
  pattern = "\\s+",
  lit = /\s+/g,
  cons = new RegExp(pattern,flags)
  // !!! Previous revision called .compile without arguments, compiling new, EMPTY regexp.
  // !!! That's why it had noticeable difference in speed. Same for cons_comp.
  // Chrome does not return new reference from compile, so you need to assign regexp to variable before calling compile on it.
  var lit_comp = (/\s+/g)
  lit_comp.compile(pattern, flags)
  var cons_comp=new RegExp(pattern,flags)
  cons_comp.compile(pattern, flags)
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
literal
test_str.replace(/\s+/g, '');
ready
constructor
test_str.replace(new RegExp('\\s+', 'g'), '');
ready
literal reference
test_str.replace(lit, '');
ready
constr. string ref
test_str.replace(new RegExp(pattern, flags), '');
ready
constr. pattern ref.
test_str.replace(cons, '');
ready
literal ref. w/compile
test_str.replace(lit_comp, '');
ready
constr. ref. w/compile
test_str.replace(cons_comp, '');
ready

Revisions

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