reuse regexp in trim

Benchmark created by lowik on


Preparation HTML

<script>
  function trim1(str) {
      return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  }
  
  function trim2(str) {
      var start = /^\s\s*/,
          end = /\s\s*$/;
  
      trim2 = function(str) {
          return str.replace(start, '').replace(end, '');
      }
  
      return trim2(str);
  }
  
  s = "     one two three   "
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
naive
trim1(s);
ready
reuse regexps
trim2(s);
ready

Revisions

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