jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
<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>
Ready to run.
Test | Ops/sec | |
---|---|---|
[node]path.normalizeArray |
| ready |
normalizeArray1 |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.