RegEx vs regex literal (v5)

Revision 5 of this benchmark created on


Description

Rev 5: Removed .compile tests. I was using it wrong (thanks to rev2 author for pointing it out!), and it's use shouldn't be encouraged any longer. (currently deprecated).

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),
      cons_lit=new RegExp(lit);
</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
constr. literal
test_str.replace(cons_lit, '');
ready
constr. as func str. ref
test_str.replace(RegExp(pattern, flags), '');
ready

Revisions

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