perf: don't manipulate an empty value#25647
Conversation
|
|
|
@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.
I don't see an issue with the previous code here...
There was a problem hiding this comment.
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.
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>
|
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