I\'m trying to write a function that measures the execution time of another function:
export class Profiler {
public measureSyncFunc(fn: () => any): P
Don't use setInterval to count milliseconds (It's inaccurate, lags, drifts and has a minimum interval of about 4ms). Just get two timestamps before and after the execution.
function measureASyncFunc(fn: () => Promise): Promise {
const start = Date.now();
return fn.catch(() => {}).then(() => {
const end = Date.now();
const elapsed = end-start;
return elapsed;
});
}
For higher accuracy, replace Date.now by performance.now.