Skip to content

Commit

Permalink
Merge pull request #28 from footageone/add-constructor-name
Browse files Browse the repository at this point in the history
Add constructor name to output
  • Loading branch information
NetanelBasal authored Feb 6, 2021
2 parents bdff5e3 + 9f5875b commit 57bf059
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/measure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ import * as util from 'util'

export function measure(target: any, propertyKey: string, descriptor: PropertyDescriptor): any {
const originalMethod = descriptor.value;
const name = target && target.constructor && target.constructor.name ? `${target.constructor.name}.${propertyKey}` : propertyKey;

if (util.types.isAsyncFunction(originalMethod)) {
descriptor.value = async function (...args: any): Promise<any> {
const start = performance.now();
const result = await originalMethod.apply(this, args);
const end = performance.now();
console.log(`Call to ${propertyKey} took ${(end - start).toFixed(2)} milliseconds.`);
console.log(`Call to ${name} took ${(end - start).toFixed(2)} milliseconds.`);
return result;
}
} else {
descriptor.value = function (...args) {
const start = performance.now();
const result = originalMethod.apply(this, args);
const end = performance.now();
console.log(`Call to ${propertyKey} took ${(end - start).toFixed(2)} milliseconds.`);
console.log(`Call to ${name} took ${(end - start).toFixed(2)} milliseconds.`);
return result;
};
}
Expand Down

0 comments on commit 57bf059

Please sign in to comment.