RegEx vs regex literal

Benchmark created on


Preparation HTML

<script>
  var test_str = " a aa  b bbb b bb       b",
      flags = 'g',
      pattern = "\s+",
      lit = /\s+/g,
      lit_comp = (/\s+/g).compile(),
      cons = new RegExp(pattern,flags),
      cons_comp=(new RegExp(pattern,flags)).compile();
</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.