-
-
Notifications
You must be signed in to change notification settings - Fork 7
Open
Description
Lets take the isAbsolute function of posix in the path module.
/**
* @param {string} path
* @returns {boolean}
*/
isAbsolute(path) {
validateString(path, 'path');
return path.length > 0 &&
StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH;
},
Benchmark it:
./node benchmark/path/isAbsolute-posix.js
path/isAbsolute-posix.js n=100000 path="": 26,199,553.088023424
path/isAbsolute-posix.js n=100000 path=".": 19,512,560.625525866
path/isAbsolute-posix.js n=100000 path="/foo/bar": 15,003,530.33068681
path/isAbsolute-posix.js n=100000 path="/baz/..": 20,841,496.252698973
path/isAbsolute-posix.js n=100000 path="bar/baz": 25,044,717.342815597
inline validateString
/**
* @param {string} path
* @returns {boolean}
*/
isAbsolute(path) {
if (typeof path !== 'string')
throw new ERR_INVALID_ARG_TYPE('path', 'string', path);
return path.length > 0 &&
StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH;
},
./node benchmark/path/isAbsolute-posix.js
path/isAbsolute-posix.js n=100000 path="": 49,143,043.605605446
path/isAbsolute-posix.js n=100000 path=".": 36,665,695.025748484
path/isAbsolute-posix.js n=100000 path="/foo/bar": 20,950,367.322790273
path/isAbsolute-posix.js n=100000 path="/baz/..": 22,806,405.772027623
path/isAbsolute-posix.js n=100000 path="bar/baz": 41,722,470.470921524
Annoying...
Metadata
Metadata
Assignees
Labels
No labels