Skip to content

perf(kit): avoid duplicate join operation#24717

Merged
danielroe merged 2 commits intonuxt:mainfrom
GalacticHypernova:patch-11
Dec 14, 2023
Merged

perf(kit): avoid duplicate join operation#24717
danielroe merged 2 commits intonuxt:mainfrom
GalacticHypernova:patch-11

Conversation

@GalacticHypernova
Copy link
Copy Markdown
Contributor

@GalacticHypernova GalacticHypernova commented Dec 12, 2023

🔗 Linked issue

#24707

❓ 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

There is a duplicate filesystem operation in the nuxt module resolving process. This PR saves the reference to prevent the redundancy and thereby improve the performance.

NOTE: While the performance improvement here is way more miniscule, it still exists. I couldn't test it with the methods nuxt uses (I will make a benchmark with that soon) but with native fs's methods there's a ~10ms decrease in execution time, which is much more noticeable is longer iterations (~8.38% performance increase in 1000 iterations):

import { existsSync, promises as fsp } from "node:fs"
import { dirname, join } from "node:path"
import { fileURLToPath } from "node:url"
async function main() {
    async function test() {
        let buildTimeModuleMeta
        if (existsSync(join(dirname(fileURLToPath(import.meta.url)), 'Test2.json'))) {
            buildTimeModuleMeta = JSON.parse(await fsp.readFile(join(dirname(fileURLToPath(import.meta.url)), 'Test2.json'), 'utf-8'))
        }
    }
    async function test2() {
        let buildTimeModuleMeta
        const modB = join(dirname(fileURLToPath(import.meta.url)), 'Test2.json')
        if (existsSync(modB)) {
            buildTimeModuleMeta = JSON.parse(await fsp.readFile(modB, 'utf-8'))
        }
    }
    console.time("Current")
    for (let index = 0; index < 1000; index++) {
        await test()
    }
    console.timeEnd("Current")
    console.time("New")
    for (let index = 0; index < 1000; index++) {
        await test2()
    }
    console.timeEnd("New")
}
main()
//Test.json
{
"name":"test"
}

image

I don't know if pathe implements it differently, so I don't mind this not being merged for the time being (until I make the tests), however even my mind tells me a duplicate operation, even if insanely fast, will still take longer than saving a reference to it.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@bolt-new-by-stackblitz
Copy link
Copy Markdown

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

@GalacticHypernova GalacticHypernova changed the title perf(kit): cache the module path perf(kit): save the module path Dec 12, 2023
@github-actions github-actions bot added the 3.x label Dec 12, 2023
@danielroe danielroe changed the title perf(kit): save the module path perf(kit): avoid duplicate join operation Dec 13, 2023
@danielroe danielroe merged commit 1711c33 into nuxt:main Dec 14, 2023
@github-actions github-actions bot mentioned this pull request Dec 14, 2023
@GalacticHypernova GalacticHypernova deleted the patch-11 branch December 15, 2023 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants