Skip to content

Instantly share code, notes, and snippets.

@andreafalzetti
Created December 4, 2018 09:08
Show Gist options
  • Save andreafalzetti/7cf1c8ca35e931496ff6adcefec4136c to your computer and use it in GitHub Desktop.
Save andreafalzetti/7cf1c8ca35e931496ff6adcefec4136c to your computer and use it in GitHub Desktop.
const PerformanceProfiler = () => {
const performance = {};
const start = metricName => {
if (!performance[metricName]) {
performance[metricName] = {};
}
performance[metricName].startAt = process.hrtime();
};
const end = metricName => {
performance[metricName].endAt = process.hrtime(
performance[metricName].startAt
);
performance[metricName].timeTaken =
performance[metricName].endAt[0] * 1000 +
performance[metricName].endAt[1] / 1e6;
};
const output = (round = false) =>
Object.keys(performance).reduce((acc, metric) => {
if (round) {
acc[metric] = Math.floor(performance[metric].timeTaken);
} else {
acc[metric] = performance[metric].timeTaken;
}
return acc;
}, {});
return { start, end, output };
};
profiler.start('fetch-data');
// do the query
profiler.stop('fetch-data');
const perf = profiler.output();
console.log(perf);
// { fetch-data: xxx }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment