Remove leading and trailing slashes (v5)

Revision 5 of this benchmark created by Yannick Albert on


Setup

var testString = '///foo/is/not/equal/to/bar///';
  
  function normalize1(path) {
    return path.replace(/^\/+|\/+$/g, '');
  }
  
  function normalize2(path) {
    while (path.charAt(0) === '/') {
      path = path.slice(1)
    }
    while (path.slice(-1) === '/') {
      path = path.slice(0, -1)
    }
  }
  
  function normalize3(path) {
    var a = 0;
    var b = path.length - 1;
    while (path.charAt(a) === '/' && ++a);
    while (path.charAt(b) === '/' && --b);
    path = path.slice(a, b + 1);
  }
  
  function normalize4(path) {
    var a = 0;
    var b = path.length - 1;
    while (path.charCodeAt(a) === 47 && ++a);
    while (path.charCodeAt(b) === 47 && --b);
    path = path.slice(a, b + 1);
  }

Test runner

Ready to run.

Testing in
TestOps/sec
normalize1
normalize1(testString);
ready
normalize2
normalize2(testString);
ready
normalize3
normalize3(testString);
ready
normalize4
normalize4(testString);
ready

Revisions

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

  • Revision 5: published by Yannick Albert on