Skip to content

Commit

Permalink
Fix perf timers for object hooks (#4707)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Nov 11, 2022
1 parent 7307bfa commit e228fc7
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 5 deletions.
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');

0 comments on commit e228fc7

Please sign in to comment.