From 609879abd859530fed205215fd7a664677e11184 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Mon, 7 Nov 2022 06:37:09 +0100 Subject: [PATCH] Fix perf timers for object hooks --- build-plugins/esm-dynamic-import.ts | 3 +++ scripts/perf.js | 6 ++++-- src/utils/timers.ts | 13 ++++++++++--- .../_config.js | 0 .../foo.js | 0 .../main.js | 0 .../perf-supports-object-hooks/_config.js | 16 ++++++++++++++++ .../samples/perf-supports-object-hooks/main.js | 1 + 8 files changed, 34 insertions(+), 5 deletions(-) rename test/function/samples/{adds-plugin-context-to-plugins => perf-adds-plugin-context-to-plugins}/_config.js (100%) rename test/function/samples/{adds-plugin-context-to-plugins => perf-adds-plugin-context-to-plugins}/foo.js (100%) rename test/function/samples/{adds-plugin-context-to-plugins => perf-adds-plugin-context-to-plugins}/main.js (100%) create mode 100644 test/function/samples/perf-supports-object-hooks/_config.js create mode 100644 test/function/samples/perf-supports-object-hooks/main.js diff --git a/build-plugins/esm-dynamic-import.ts b/build-plugins/esm-dynamic-import.ts index 073852d18fd..956310aa046 100644 --- a/build-plugins/esm-dynamic-import.ts +++ b/build-plugins/esm-dynamic-import.ts @@ -18,6 +18,9 @@ export default function esmDynamicImport(): Plugin { importsFound++; return { left: 'import(', right: ')' }; } + }, + renderStart() { + importsFound = 0; } }; } diff --git a/scripts/perf.js b/scripts/perf.js index 01047f09d1a..66501115808 100644 --- a/scripts/perf.js +++ b/scripts/perf.js @@ -148,7 +148,7 @@ function printMeasurements(average, existingAverage, filter = /.*/) { } function clearLines(numberOfLines) { - console.info('\33[A' + '\33[2K\33[A'.repeat(numberOfLines)); + console.info('\u001B[A' + '\u001B[2K\u001B[A'.repeat(numberOfLines)); } function getExistingTimings() { @@ -203,4 +203,6 @@ function getFormattedMemory(currentMemory, persistedMemory = currentMemory) { return color(formattedMemory); } -const identity = x => x; +function identity(x) { + return x; +} diff --git a/src/utils/timers.ts b/src/utils/timers.ts index 974073dfe9c..7c44560ca20 100644 --- a/src/utils/timers.ts +++ b/src/utils/timers.ts @@ -113,14 +113,21 @@ function getPluginWithTimers(plugin: any, index: number): Plugin { } timerLabel += ` - ${hook}`; - const hookFunction = plugin[hook]; - - plugin[hook] = function (...parameters: readonly unknown[]) { + const handler = function (this: any, ...parameters: readonly unknown[]) { timeStart(timerLabel, 4); const result = hookFunction.apply(this, parameters); timeEnd(timerLabel, 4); return result; }; + + let hookFunction: any; + if (typeof plugin[hook].handler === 'function') { + hookFunction = plugin[hook].handler; + plugin[hook].handler = handler; + } else { + hookFunction = plugin[hook]; + plugin[hook] = handler; + } } } return plugin; diff --git a/test/function/samples/adds-plugin-context-to-plugins/_config.js b/test/function/samples/perf-adds-plugin-context-to-plugins/_config.js similarity index 100% rename from test/function/samples/adds-plugin-context-to-plugins/_config.js rename to test/function/samples/perf-adds-plugin-context-to-plugins/_config.js diff --git a/test/function/samples/adds-plugin-context-to-plugins/foo.js b/test/function/samples/perf-adds-plugin-context-to-plugins/foo.js similarity index 100% rename from test/function/samples/adds-plugin-context-to-plugins/foo.js rename to test/function/samples/perf-adds-plugin-context-to-plugins/foo.js diff --git a/test/function/samples/adds-plugin-context-to-plugins/main.js b/test/function/samples/perf-adds-plugin-context-to-plugins/main.js similarity index 100% rename from test/function/samples/adds-plugin-context-to-plugins/main.js rename to test/function/samples/perf-adds-plugin-context-to-plugins/main.js diff --git a/test/function/samples/perf-supports-object-hooks/_config.js b/test/function/samples/perf-supports-object-hooks/_config.js new file mode 100644 index 00000000000..84e0155413e --- /dev/null +++ b/test/function/samples/perf-supports-object-hooks/_config.js @@ -0,0 +1,16 @@ +module.exports = { + description: 'Supports object hooks with perf=true', + options: { + perf: true, + plugins: [ + { + transform: { + order: 'pre', + handler(code) { + return code.replace('FOO', 'BAR'); + } + } + } + ] + } +}; diff --git a/test/function/samples/perf-supports-object-hooks/main.js b/test/function/samples/perf-supports-object-hooks/main.js new file mode 100644 index 00000000000..b52321a9995 --- /dev/null +++ b/test/function/samples/perf-supports-object-hooks/main.js @@ -0,0 +1 @@ +assert.strictEqual('FOO', 'BAR');