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
perf: don't manipulate an empty value #25647
Conversation
Run & review this pull request in StackBlitz Codeflow. |
@danielroe Is this something I accidentally did with one of the changes? |
const rules = defu({}, ...routeRulesMatcher.matchAll(url).reverse()) as Record<string, any> | ||
if (!rules.prerender) { | ||
prerenderedRoutes.add(url) | ||
if (nitro._prerenderedRoutes?.length) { |
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 don't see an issue with the previous code 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.
This is kind of the same principle as everything else, if the array is empty the performance overhead to begin checking the iteration (even if exiting immediately) is much bigger than checking for a truthy length. I'll be able to append benchmark results when I get on PC but there was a notable increase in performance.
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.
Update: here it is.
const routeRulesMatcher=""
function defu(table, ...args){
}
const prerenderedRoutes=new Set()
const payloadSuffix=""
const nitro={
_prerenderedRoutes:[]
}
console.time("current")
for (let index = 0; index < 100000; index++) {
for (const route of nitro._prerenderedRoutes || []) {
if (!route.error && route.route.endsWith(payloadSuffix)) {
const url = route.route.slice(0, -payloadSuffix.length) || '/'
const rules = defu({}, ...routeRulesMatcher.matchAll(url).reverse())
if (!rules.prerender) {
prerenderedRoutes.add(url)
}
}
}
}
console.timeEnd("current")
console.time("PR")
for (let index = 0; index < 100000; index++) {
if (nitro._prerenderedRoutes?.length) {
for (const route of nitro._prerenderedRoutes) {
if (!route.error && route.route.endsWith(payloadSuffix)) {
const url = route.route.slice(0, -payloadSuffix.length) || '/'
const rules = defu({}, ...routeRulesMatcher.matchAll(url).reverse())
if (!rules.prerender) {
prerenderedRoutes.add(url)
}
}
}
}
}
console.timeEnd("PR")
Co-authored-by: Daniel Roe <daniel@roe.dev>
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.
Thank you β€οΈ
Always glad to help! |
π Linked issue
β Type of change
π Description
We should not iterate and make O(n) operations on an empty values (strings, arrays), just to receive the same empty value back.
π Checklist