refactor(kit,nuxt): use performance.now to measure time

This commit is contained in:
Daniel Roe 2024-07-02 12:12:56 +01:00
parent ffab26b8ed
commit d14f7ec460
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B
2 changed files with 6 additions and 6 deletions

View File

@ -71,10 +71,10 @@ export function defineNuxtModule<OptionsT extends ModuleOptions> (definition: Mo
// Call setup // Call setup
const key = `nuxt:module:${uniqueKey || (Math.round(Math.random() * 10000))}` const key = `nuxt:module:${uniqueKey || (Math.round(Math.random() * 10000))}`
const mark = performance.mark(key) const start = performance.now()
const res = await module.setup?.call(null as any, _options, nuxt) ?? {} const res = await module.setup?.call(null as any, _options, nuxt) ?? {}
const perf = performance.measure(key, mark.name) const perf = performance.now() - start
const setupTime = Math.round((perf.duration * 100)) / 100 const setupTime = Math.round((perf * 100)) / 100
// Measure setup time // Measure setup time
if (setupTime > 5000 && uniqueKey !== '@nuxt/telemetry') { if (setupTime > 5000 && uniqueKey !== '@nuxt/telemetry') {

View File

@ -62,7 +62,7 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?:
async function processTemplate (template: ResolvedNuxtTemplate) { async function processTemplate (template: ResolvedNuxtTemplate) {
const fullPath = template.dst || resolve(nuxt.options.buildDir, template.filename!) const fullPath = template.dst || resolve(nuxt.options.buildDir, template.filename!)
const mark = performance.mark(fullPath) const start = performance.now()
const oldContents = nuxt.vfs[fullPath] const oldContents = nuxt.vfs[fullPath]
const contents = await compileTemplate(template, templateContext).catch((e) => { const contents = await compileTemplate(template, templateContext).catch((e) => {
logger.error(`Could not compile template \`${template.filename}\`.`) logger.error(`Could not compile template \`${template.filename}\`.`)
@ -85,8 +85,8 @@ export async function generateApp (nuxt: Nuxt, app: NuxtApp, options: { filter?:
changedTemplates.push(template) changedTemplates.push(template)
} }
const perf = performance.measure(fullPath, mark.name) const perf = performance.now() - start
const setupTime = Math.round((perf.duration * 100)) / 100 const setupTime = Math.round((perf * 100)) / 100
if (nuxt.options.debug || setupTime > 500) { if (nuxt.options.debug || setupTime > 500) {
logger.info(`Compiled \`${template.filename}\` in ${setupTime}ms`) logger.info(`Compiled \`${template.filename}\` in ${setupTime}ms`)