Skip to content
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

Merged
merged 45 commits into from Feb 26, 2024
Merged

Conversation

GalacticHypernova
Copy link
Contributor

@GalacticHypernova GalacticHypernova commented Feb 5, 2024

πŸ”— Linked issue

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to 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

  • I have linked an issue or discussion.
  • I have added tests (if possible).
  • I have updated the documentation accordingly.

Copy link

stackblitz bot commented Feb 5, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@GalacticHypernova GalacticHypernova marked this pull request as draft February 5, 2024 20:32
@GalacticHypernova GalacticHypernova changed the title perf(nuxt): don't iterate over an empty string perf: don't iterate over an empty string Feb 5, 2024
@GalacticHypernova GalacticHypernova marked this pull request as ready for review February 5, 2024 20:47
@GalacticHypernova GalacticHypernova marked this pull request as draft February 5, 2024 20:47
@GalacticHypernova GalacticHypernova marked this pull request as ready for review February 5, 2024 21:18
@GalacticHypernova GalacticHypernova changed the title perf: don't iterate over an empty string perf: don't manipulate an empty string Feb 6, 2024
@GalacticHypernova GalacticHypernova marked this pull request as draft February 9, 2024 13:49
@GalacticHypernova
Copy link
Contributor Author

GalacticHypernova commented Feb 9, 2024

@danielroe
The failing tests don't really have anything to do with the PR... I haven't changed anything in the component island URL's.

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) {
Copy link
Member

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...

Copy link
Contributor Author

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.

Copy link
Contributor Author

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")

image

@GalacticHypernova GalacticHypernova marked this pull request as ready for review February 18, 2024 22:05
Copy link
Member

@danielroe danielroe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you ❀️

@danielroe danielroe merged commit ff1bb56 into nuxt:main Feb 26, 2024
34 checks passed
@github-actions github-actions bot mentioned this pull request Feb 26, 2024
@GalacticHypernova
Copy link
Contributor Author

Always glad to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants