normalize-path

Benchmark created by rsms on


Preparation HTML

<script>
  // path.normalizeArray
  
  function normalizeArray_node(parts, keepBlanks) {
   var directories = [],
       prev;
   for (var i = 0, l = parts.length - 1; i <= l; i++) {
    var directory = parts[i];
  
    // if it's blank, but it's not the first thing, and not the last thing, skip it.
    if (directory === "" && i !== 0 && i !== l && !keepBlanks) continue;
  
    // if it's a dot, and there was some previous dir already, then skip it.
    if (directory === "." && prev !== undefined) continue;
  
    // if it starts with "", and is a . or .., then skip it.
    if (directories.length === 1 && directories[0] === "" && (
    directory === "." || directory === "..")) continue;
  
    if (
    directory === ".." && directories.length && prev !== ".." && prev !== "." && prev !== undefined && (prev !== "" || keepBlanks)) {
     directories.pop();
     prev = directories.slice(-1)[0]
    } else {
     if (prev === ".") directories.pop();
     directories.push(directory);
     prev = directory;
    }
   }
   return directories;
  }
  
  // alternate implementation
  
  function normalizeArray1(v, keepBlanks) {
   var L = v.length,
       dst = new Array(L),
       dsti = 0,
       i = 0,
       part, negatives = 0,
       isRelative = (L && v[0] !== '');
   for (; i < L; ++i) {
    part = v[i];
    if (part === '..') {
     if (dsti > 1) {
      --dsti;
     } else if (isRelative) {
      ++negatives;
     } else {
      dst[0] = '';
     }
    } else if (part !== '.' && (dsti === 0 || keepBlanks || part !== '')) {
     dst[dsti++] = part;
    }
   }
   if (negatives) {
    dst[--negatives] = dst[dsti - 1];
    dsti = negatives + 1;
    while (negatives--) {
     dst[negatives] = '..';
    }
   }
   dst.length = dsti;
   return dst;
  }
  
  function normalizePath(normalizeArray, path, keepBlanks) {
   return normalizeArray(path.split("/"), keepBlanks).join("/");
  }
  
  // -----------------------------------
  
  function assert(cond, tag) {
   if (!cond) throw new Error('assertion error' + (tag ? ' -- ' + tag : ''));
  }
  
  function runTests(normalizeArray) {
   assert(normalizePath(normalizeArray, "./fixtures///b/../b/c.js"), "fixtures/b/c.js");
   assert(normalizePath(normalizeArray, "./fixtures///b/../b/c.js", true), "fixtures///b/c.js");
   assert(normalizePath(normalizeArray, "/foo/../../../bar"), "/bar");
   assert(normalizePath(normalizeArray, "./foo/../../../bar"), "../../bar");
   assert(normalizePath(normalizeArray, "a//b//../b", true), "a//b/b");
   assert(normalizePath(normalizeArray, "a//b//../b"), "a/b");
   assert(normalizePath(normalizeArray, "a//b//./c", true), "a//b//c");
   assert(normalizePath(normalizeArray, "a//b//./c"), "a/b/c");
   assert(normalizePath(normalizeArray, "a//b//.", true), "a//b/");
   assert(normalizePath(normalizeArray, "a//b//."), "a/b");
   assert(normalizeArray(["fixtures", "b", "", "..", "b", "c.js"].join('/')), ["fixtures", "b", "c.js"].join('/'));
   assert(normalizeArray(["fixtures", "", "b", "..", "b", "c.js"].join('/'), true), ["fixtures", "", "b", "c.js"].join('/'));
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
[node]path.normalizeArray
runTests(normalizeArray_node);
ready
normalizeArray1
runTests(normalizeArray1);
ready

Revisions

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