-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
url: add fast path to getPathFromURL decoder #60749
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
base: main
Are you sure you want to change the base?
Conversation
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #60749 +/- ##
==========================================
+ Coverage 88.54% 88.55% +0.01%
==========================================
Files 703 703
Lines 208222 208256 +34
Branches 40140 40152 +12
==========================================
+ Hits 184360 184420 +60
+ Misses 15882 15836 -46
- Partials 7980 8000 +20
🚀 New features to boost your workflow:
|
lib/internal/url.js
Outdated
| pathname = SideEffectFreeRegExpPrototypeSymbolReplace(FORWARD_SLASH, pathname, '\\'); | ||
| pathname = decodeURIComponent(pathname); | ||
| // Fast-path: if there is no percent-encoding, avoid decodeURIComponent. | ||
| if (StringPrototypeIndexOf(pathname, '%') !== -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 indexOf?
| if (StringPrototypeIndexOf(pathname, '%') !== -1) { | |
| if (StringPrototypeIncludes(pathname, '%')) { |
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 a convention from Fastify, we prefered indexOf as it was faster until node 22. The difference was even bigger in earlier node versions.
For instance, in node 20:
Starting benchmark...
Searching for "lazy dog" in "The quick brown fox jumps over the lazy dog. This is a long sentence used for benchmarking string search methods."
String#includes x 952,187,610 ops/sec ±0.81% (98 runs sampled)
String#indexOf x 960,298,473 ops/sec ±0.16% (102 runs sampled)
-----------------------------------
Fastest is String#indexOfbenchmark:
const Benchmark = require('benchmark');
// --- Setup ---
const suite = new Benchmark.Suite;
// The string to search within
const text = 'The quick brown fox jumps over the lazy dog. This is a long sentence used for benchmarking string search methods.';
// The substring to find
const substring = 'lazy dog';
// --- Benchmark Suite ---
console.log('Starting benchmark...');
console.log(`Searching for "${substring}" in "${text}"\n`);
suite
.add('String#includes', function() {
text.includes(substring);
})
.add('String#indexOf', function() {
text.indexOf(substring) !== -1;
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('\n-----------------------------------');
console.log('Fastest is ' + this.filter('fastest').map('name'));
console.log('-----------------------------------');
})
.run();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 can use includes though if you're fine with the small difference for node 20
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.
In latest V8 it seems to have gone away though, so I changed to includes:
url/whatwg-url-to-and-from-path.js n=5000000 input="file:///dev/null" method="fileURLToPath": 3,903,087.8889524657
url/whatwg-url-to-and-from-path.js n=5000000 input="file:///dev/null?key=param&bool" method="fileURLToPath": 3,469,377.354475161
url/whatwg-url-to-and-from-path.js n=5000000 input="file:///dev/null?key=param&bool#hash" method="fileURLToPath": 3,498,714.659701892
branch:
main:
The majority of file paths won't need decoding, so this PR avoids calling
decodeURIComponentwhen the path is pure ASCII with no encodings