-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
url: improve performance of the format function #57099
base: main
Are you sure you want to change the base?
Conversation
Review requested:
|
64039ec
to
bdd5eb4
Compare
lib/url.js
Outdated
host = '//'; | ||
} | ||
} | ||
|
||
search = search.replaceAll('#', '%23'); | ||
// Escape '#' in search. | ||
if (search.indexOf('#') !== -1) { |
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.
Why didn't you use primordials here?
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.
Maybe I'm missing something but in this file the usage of primordials seems inconsistent now
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 didn't use primordials because I forgot one spot :) changing it now!
I had the same doubt as you, the file is using very few primordials. I will create another PR to convert the whole file to be using them as much as possible
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.
Ah no problem I was just curious if there was a reason :)
lib/url.js
Outdated
search = search.replaceAll('#', '%23'); | ||
// Escape '#' in search. | ||
if (search.indexOf('#') !== -1) { | ||
search = search.replace(/#/g, '%23'); |
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 guess this got slower when # exists, but looks like an ok compromise
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.
search = search.replace(/#/g, '%23'); | |
search = search.replaceAll('#', '%23'); |
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.
By the way this is because replaceAll has similar or better performance than a global static regexp, because it doesn't need to be compiled
To lower regexp usage I had made a PR replacing all these a while ago, so I think you can safely replace with replaceAll
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.
fascinating, it makes sense :) updated it with StringPrototypeReplaceAll
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #57099 +/- ##
=======================================
Coverage 89.09% 89.09%
=======================================
Files 665 665
Lines 193249 193252 +3
Branches 37231 37222 -9
=======================================
+ Hits 172175 172184 +9
+ Misses 13802 13799 -3
+ Partials 7272 7269 -3
|
bdd5eb4
to
247e1a2
Compare
247e1a2
to
1173315
Compare
@nodejs/performance can someone start a url benchmark ci? |
https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1657/
|
|
As part of my ongoing review of the
assert
codebase, I started examining thelib/url.js
file and optimizing some straightforward code that could be further refined for better performance.Here are the benchmark results for the
benchmarks/url/url-format.js
file:For type="slashes":
123% improvement
For type="file":
50% improvement