-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
path: refactor for performance and consistency #1778
Conversation
@@ -37,6 +37,29 @@ function normalizeArray(parts, allowAboveRoot) { | |||
return res; | |||
} | |||
|
|||
// returns an array with empty elements removed from either end of the input |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First letter should be capitalized.
Are there any API changes? (seeing that the tests were modified) |
@silverwind I'm not sure if this counts as an API change, but as discussed here, if a user passes in an object where the |
I'm keen on getting the benchmark in ( |
@jbergstroem It looks like each |
Looks like #1752 conflicts with this. I will remove the Btw,
Is also valid for this PR. |
LGTM |
Why using |
@jbergstroem I added some benchmarks. Do those look about right? |
@woollybogger apologies for the late reply. LGTM. CI here: https://jenkins-iojs.nodesource.com/job/iojs+any-pr+multi/827/ |
@jbergstroem A couple unrelated (and possibly flaky) tests failed. Does that matter? |
@woollybogger no, that's something we're working on improving. I can't access the old results -- can you rebase on top of latest master so we can get a new run going? You might want to see if anyone else wants to review before I/someone else look at landing it. @ChALkeR? |
Rebased |
(never mind the windows timeouts) |
The main part ( Minor thing: you replace single-char posix.isAbsolute = function(path) {
assertPath(path);
return path.charAt(0) === '/';
}; Could you fix that for consistency? |
@ChALkeR Fixed. |
@@ -37,6 +37,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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure if returning the original array and a new array based on conditions is a good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just a little helper function. There's no point in making a copy of an array when the original is all you need.
bump 🚅 |
isUnc: isUnc, | ||
isAbsolute: isUnc || !!result[2], // UNC paths are always absolute | ||
tail: result[3] | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could use shorthand properties here:
return {
device,
isUnc,
isAbsolute: isUnc || !!result[2], // UNC paths are always absolute
tail: result[3]
};
LGTM, few nits and a question above. |
Improve performance by: + Not leaking the `arguments` object! + Getting the last character of a string by index, instead of with `.substr()` or `.slice()` Improve code consistency by: + Using `[]` instead of `.charAt()` where possible + Using a function declaration instead of a var declaration + Using `.slice()` with clearer arguments + Checking if `dir` is truthy in `win32.format` (added tests for this) Improve both by: + Making the reusable `trimArray()` function + Standardizing getting certain path statistics with the new `win32StatPath()` function
Path functions being benchmarked are: * format * isAbsolute * join * normalize * relative * resolve
Updated |
LGTM |
Improve performance by: + Not leaking the `arguments` object! + Getting the last character of a string by index, instead of with `.substr()` or `.slice()` Improve code consistency by: + Using `[]` instead of `.charAt()` where possible + Using a function declaration instead of a var declaration + Using `.slice()` with clearer arguments + Checking if `dir` is truthy in `win32.format` (added tests for this) Improve both by: + Making the reusable `trimArray()` function + Standardizing getting certain path statistics with the new `win32StatPath()` function PR-URL: #1778 Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: Roman Reiss <[email protected]>
Path functions being benchmarked are: * format * isAbsolute * join * normalize * relative * resolve PR-URL: #1778 Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: Roman Reiss <[email protected]>
Improve performance by: + Not leaking the `arguments` object! + Getting the last character of a string by index, instead of with `.substr()` or `.slice()` Improve code consistency by: + Using `[]` instead of `.charAt()` where possible + Using a function declaration instead of a var declaration + Using `.slice()` with clearer arguments + Checking if `dir` is truthy in `win32.format` (added tests for this) Improve both by: + Making the reusable `trimArray()` function + Standardizing getting certain path statistics with the new `win32StatPath()` function PR-URL: nodejs#1778 Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: Roman Reiss <[email protected]>
Path functions being benchmarked are: * format * isAbsolute * join * normalize * relative * resolve PR-URL: nodejs#1778 Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: Roman Reiss <[email protected]>
Convergence PR prompted by #35
Original PR: nodejs/node-v0.x-archive#9289
Original PR message (slightly modified):
Improve performance by:
arguments
object inwin32.join
.substr()
or.slice()
Improve code consistency by:
[]
instead of.charAt()
where possible.slice()
with clearer argumentsImprove both by:
trimArray()
functionwin32StatPath()
functionBenchmarks: (higher is better)
Benchmark code (gist)