regex vs. split (v2)

Revision 2 of this benchmark created by Kael on


Description

Kael: 2 reasons make dirname2 slow: 1. match group(s) 2. string replacement 3. string extraction

so, i try to make it nearly 3 times faster.

Preparation HTML

<script>
  function dirname(path) {
   var s = path.split('/').slice(0, -1).join('/');
   return s ? s : '.';
  }
  
  function dirname2(path) {
   var s = ('./' + path).replace(/(.*)?\/.*/, '$1').substring(2);
   return s ? s : '.';
  }
  
  function dirname3(path) {
   var s = (path).match(/.*(?=\/.*$)/);
   return (s ? s[0] : '.') + '/';
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
split
dirname('a/b/c.js');
dirname('a/b/c');
dirname('a/b/c/');
dirname('d.js');
ready
regex
dirname2('a/b/c.js');
dirname2('a/b/c');
dirname2('a/b/c/');
dirname2('d.js');
ready
regex match
dirname3('a/b/c.js');
dirname3('a/b/c');
dirname3('a/b/c/');
dirname3('d.js');
ready

Revisions

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