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

Fix perf script for object hooks #4707

Merged
merged 1 commit into from Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions build-plugins/esm-dynamic-import.ts
Expand Up @@ -18,6 +18,9 @@ export default function esmDynamicImport(): Plugin {
importsFound++;
return { left: 'import(', right: ')' };
}
},
renderStart() {
importsFound = 0;
}
};
}
6 changes: 4 additions & 2 deletions scripts/perf.js
Expand Up @@ -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() {
Expand Down Expand Up @@ -203,4 +203,6 @@ function getFormattedMemory(currentMemory, persistedMemory = currentMemory) {
return color(formattedMemory);
}

const identity = x => x;
function identity(x) {
return x;
}
13 changes: 10 additions & 3 deletions src/utils/timers.ts
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions 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');
}
}
}
]
}
};
1 change: 1 addition & 0 deletions test/function/samples/perf-supports-object-hooks/main.js
@@ -0,0 +1 @@
assert.strictEqual('FOO', 'BAR');