Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
path: refactor for performance and consistency
Browse files Browse the repository at this point in the history
Improve performance by:
+ Not running the same code twice (in win32.resolve and .normalize)
+ Not leaking the `arguments` object!
+ Getting the last character of a string by index, instead of
  with `.substr()`

Improve code consistency by:
+ Using `[]` instead of `.charAt()` where possible
+ Using a function declaration instead of a var declaration

Improve both by:
+ Using `.slice()` instead of `.substr()`
+ Using `.slice()` with clearer arguments
+ Reusing code with the trimArray() function
  • Loading branch information
nwoltman committed Mar 17, 2015
1 parent eb2764a commit e4a5c63
Showing 1 changed file with 50 additions and 54 deletions.
104 changes: 50 additions & 54 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ function normalizeArray(parts, allowAboveRoot) {
return res;
}

// returns an array with empty elements removed from either end of the input
// array or the original array if no elements need to be removed
function trimArray(arr) {
var lastIndex = arr.length - 1;
var start = 0;
for (; start <= lastIndex; start++) {
if (arr[start])
break;
}

var end = lastIndex;
for (; end >= 0; end--) {
if (arr[end])
break;
}

if (start === 0 && end === lastIndex)
return arr;
if (start > end)
return [];
return arr.slice(start, end + 1);
}

// Regex to split a windows path into three parts: [*, device, slash,
// tail] windows-only
var splitDeviceRe =
Expand All @@ -76,9 +99,9 @@ function win32SplitPath(filename) {
return [device, dir, basename, ext];
}

var normalizeUNCRoot = function(device) {
function normalizeUNCRoot(device) {
return '\\\\' + device.replace(/^[\\\/]+/, '').replace(/[\\\/]+/g, '\\');
};
}

// path.resolve([from ...], to)
win32.resolve = function() {
Expand Down Expand Up @@ -115,8 +138,8 @@ win32.resolve = function() {

var result = splitDeviceRe.exec(path),
device = result[1] || '',
isUnc = device && device.charAt(1) !== ':',
isAbsolute = win32.isAbsolute(path),
isUnc = device && device[1] !== ':',
isAbsolute = isUnc || result[2],
tail = result[3];

if (device &&
Expand Down Expand Up @@ -161,8 +184,8 @@ win32.resolve = function() {
win32.normalize = function(path) {
var result = splitDeviceRe.exec(path),
device = result[1] || '',
isUnc = device && device.charAt(1) !== ':',
isAbsolute = win32.isAbsolute(path),
isUnc = device && device[1] !== ':',
isAbsolute = isUnc || result[2],
tail = result[3],
trailingSlash = /[\\\/]$/.test(tail);

Expand All @@ -189,20 +212,23 @@ win32.normalize = function(path) {
win32.isAbsolute = function(path) {
var result = splitDeviceRe.exec(path),
device = result[1] || '',
isUnc = !!device && device.charAt(1) !== ':';
isUnc = !!device && device[1] !== ':';
// UNC paths are always absolute
return !!result[2] || isUnc;
return isUnc || !!result[2];
};

win32.join = function() {
function f(p) {
if (!util.isString(p)) {
var paths = [];
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
if (!util.isString(arg)) {
throw new TypeError('Arguments to path.join must be strings');
}
return p;
if (arg) {
paths.push(arg);
}
}

var paths = Array.prototype.filter.call(arguments, f);
var joined = paths.join('\\');

// Make sure that the joined path doesn't start with two slashes, because
Expand Down Expand Up @@ -239,25 +265,10 @@ win32.relative = function(from, to) {
var lowerFrom = from.toLowerCase();
var lowerTo = to.toLowerCase();

function trim(arr) {
var start = 0;
for (; start < arr.length; start++) {
if (arr[start] !== '') break;
}

var end = arr.length - 1;
for (; end >= 0; end--) {
if (arr[end] !== '') break;
}

if (start > end) return [];
return arr.slice(start, end + 1);
}

var toParts = trim(to.split('\\'));
var toParts = trimArray(to.split('\\'));

var lowerFromParts = trim(lowerFrom.split('\\'));
var lowerToParts = trim(lowerTo.split('\\'));
var lowerFromParts = trimArray(lowerFrom.split('\\'));
var lowerToParts = trimArray(lowerTo.split('\\'));

var length = Math.min(lowerFromParts.length, lowerToParts.length);
var samePartsLength = length;
Expand Down Expand Up @@ -320,7 +331,7 @@ win32.dirname = function(path) {

if (dir) {
// It has a dirname, strip trailing slash
dir = dir.substr(0, dir.length - 1);
dir = dir.slice(0, -1);
}

return root + dir;
Expand Down Expand Up @@ -360,7 +371,7 @@ win32.format = function(pathObject) {

var dir = pathObject.dir;
var base = pathObject.base || '';
if (dir.slice(dir.length - 1, dir.length) === win32.sep) {
if (dir.slice(-1) === win32.sep) {
return dir + base;
}

Expand All @@ -384,7 +395,7 @@ win32.parse = function(pathString) {
}
return {
root: allParts[0],
dir: allParts[0] + allParts[1].slice(0, allParts[1].length - 1),
dir: allParts[0] + allParts[1].slice(0, -1),
base: allParts[2],
ext: allParts[3],
name: allParts[2].slice(0, allParts[2].length - allParts[3].length)
Expand Down Expand Up @@ -425,7 +436,7 @@ posix.resolve = function() {
}

resolvedPath = path + '/' + resolvedPath;
resolvedAbsolute = path.charAt(0) === '/';
resolvedAbsolute = path[0] === '/';
}

// At this point the path should be resolved to a full absolute path, but
Expand All @@ -442,7 +453,7 @@ posix.resolve = function() {
// posix version
posix.normalize = function(path) {
var isAbsolute = posix.isAbsolute(path),
trailingSlash = path.substr(-1) === '/';
trailingSlash = path && path[path.length - 1] === '/';

// Normalize the path
path = normalizeArray(path.split('/'), !isAbsolute).join('/');
Expand Down Expand Up @@ -488,23 +499,8 @@ posix.relative = function(from, to) {
from = posix.resolve(from).substr(1);
to = posix.resolve(to).substr(1);

function trim(arr) {
var start = 0;
for (; start < arr.length; start++) {
if (arr[start] !== '') break;
}

var end = arr.length - 1;
for (; end >= 0; end--) {
if (arr[end] !== '') break;
}

if (start > end) return [];
return arr.slice(start, end + 1);
}

var fromParts = trim(from.split('/'));
var toParts = trim(to.split('/'));
var fromParts = trimArray(from.split('/'));
var toParts = trimArray(to.split('/'));

var length = Math.min(fromParts.length, toParts.length);
var samePartsLength = length;
Expand Down Expand Up @@ -543,7 +539,7 @@ posix.dirname = function(path) {

if (dir) {
// It has a dirname, strip trailing slash
dir = dir.substr(0, dir.length - 1);
dir = dir.slice(0, -1);
}

return root + dir;
Expand Down Expand Up @@ -603,7 +599,7 @@ posix.parse = function(pathString) {

return {
root: allParts[0],
dir: allParts[0] + allParts[1].slice(0, allParts[1].length - 1),
dir: allParts[0] + allParts[1].slice(0, -1),
base: allParts[2],
ext: allParts[3],
name: allParts[2].slice(0, allParts[2].length - allParts[3].length)
Expand Down

0 comments on commit e4a5c63

Please sign in to comment.