Cached regex replace vs. uncached (v2)

Revision 2 of this benchmark created by Tarun on


Preparation HTML

<script>
  var regex = /[-#$^*()+[\]{}|\\,.?\s]/g,
      regex_instance = new RegExp('[-#$^*()+[\\]{}|\\\\,.?\\s]', 'g'),
      string = 'foo,bar(baz)',
      testInFunctionUsingClosureLiteral = function(string) {
          return string.replace(regex, '\\$&');
      };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Uncached literal
string.replace(/[-#$^*()+[\]{}|\\,.?\s]/g, '\\$&');
ready
Cached literal
string.replace(regex, '\\$&');
ready
Uncached instance
string.replace(new RegExp('[-#$^*()+[\\]{}|\\\\,.?\\s]', 'g'), '\\$&');
ready
Cached instance
string.replace(regex_instance, '\\$&');
ready
Cached literal in closure scope
testInFunctionUsingClosureLiteral(string);
ready

Revisions

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

  • Revision 1: published by Ryan Grove on
  • Revision 2: published by Tarun on