regex vs. split (v5)

Revision 5 of this benchmark created on


Description

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

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] : '.') + '/';
  }
  function dirname4(path) {
    var i = path.lastIndexOf("/")
    return i > -1 ? path.substring(0, i) : "./"
  }
</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
lastindexof
dirname4('a/b/c.js');
dirname4('a/b/c');
dirname4('a/b/c/');
dirname4('d.js');
ready

Revisions

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