-
Notifications
You must be signed in to change notification settings - Fork 21
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
fix(basename): ignore trailing seperator #180
Conversation
src/path.ts
Outdated
const lastSegment = normalizeWindowsPath(p) | ||
.replace(/\/+$/, "") | ||
.split("/") | ||
.pop(); |
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.
Perf: I think we can use two lines const segments = []
and another check to see if last part is empty, fallback to len-1 this way we avoid regex
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.
Perf: I think we can use two lines
const segments = []
and another check to see if last part is empty, fallback to len-1 this way we avoid regex
We need to ignore all trailing seps if there are many, see my test cases (/foo/bar////
). If we loop and pop, it will be slower than regex replace.
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 think most of the fast and normal conditions is that there is no trailing slash or just one. We don't need to pop, we can just have a reverse for loop to find the first index that has non-empty one.
Feel free to do a benchmark. What matters to me is that we don't add a perf overhead for normal conditions by adding regex replace (since this is a low level utility, it could be used hundreds or thousands of times in high level tools)
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.
@pi0 Benchmark: https://replit.com/@isukkaw/SandyPossibleLanguage
TL; DR:
endsWith
before regex replace is fastest when the input doesn't end with sep. But it is slowest if there are any- reverse loop and pop is fastest if there are seps at the end of the input. It is the second fastest if the input doesn't end with one (but it is still 10x slower than
endsWith
before regex replace)
So which one should we choose?
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.
Thanks!! See this updated repl. I have made few changes, removed other variants and add tests/adjustment that all implementations return basename for us and seems loop can be always fastest.
https://replit.com/@pi0/httpsgithubcomunjspathepull180
How do you think?
local (bun)
![image](https://private-user-images.githubusercontent.com/5158436/355791798-60cd325c-14a0-46d9-86e7-c41e451ff941.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk3NzQwMzIsIm5iZiI6MTczOTc3MzczMiwicGF0aCI6Ii81MTU4NDM2LzM1NTc5MTc5OC02MGNkMzI1Yy0xNGEwLTQ2ZDktODZlNy1jNDFlNDUxZmY5NDEucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIxNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMTdUMDYyODUyWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NjhiZTRlZGU1Yzc1MzMxMmZkOTJjMzBkOWFmOTE1ODA1OGFlNjMzNTEwNDVjYjNiODVjYzc0OWI5YjhhYjFlZiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.43z0JC1MlDaekNZYEsK2qsg-BzOQwlf9JSsgoLKXYdc)
local (node 20)
![image](https://private-user-images.githubusercontent.com/5158436/355791896-c767087d-74de-4ca1-b894-c10c0830aee4.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk3NzQwMzIsIm5iZiI6MTczOTc3MzczMiwicGF0aCI6Ii81MTU4NDM2LzM1NTc5MTg5Ni1jNzY3MDg3ZC03NGRlLTRjYTEtYjg5NC1jMTBjMDgzMGFlZTQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIxNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMTdUMDYyODUyWiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9N2U0NWQ0ZTFhYjZiNGIyMDM5N2RjYTI5MDAyM2QyNmYyY2Y0YzgyNGMyZTY3NzZlNWM2YTIzODE2M2VkY2M0MSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.O0MlwdhO3ZQPurB4X6xQZb0ro4Fqp8DNgUAt3M8A5P0)
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.
Done in c5b922e
(#180)
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.
@pi0 Just a friendly ping~
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #180 +/- ##
==========================================
- Coverage 99.45% 99.28% -0.17%
==========================================
Files 4 4
Lines 364 281 -83
Branches 114 116 +2
==========================================
- Hits 362 279 -83
Misses 2 2 ☔ View full report in Codecov by Sentry. |
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.
❤️
Fixes #179.