Skip to content

Instantly share code, notes, and snippets.

@slutske22
Last active December 26, 2021 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slutske22/8b46f7c5471ee64966ce0633c8954fb9 to your computer and use it in GitHub Desktop.
Save slutske22/8b46f7c5471ee64966ce0633c8954fb9 to your computer and use it in GitHub Desktop.
Profiler class
import fs from 'fs';
import v8profiler from 'v8-profiler-next';
export class Profiler {
constructor(options) {
const { title, active, outputDir } = options;
this.title = title;
this.active = active;
this.outputDir = outputDir;
}
start() {
if (this.active) {
v8profiler.startProfiling(this.title, true);
}
}
finish() {
if (this.active) {
const profile = v8profiler.stopProfiling(this.title);
profile.export((error, result) => {
if (error) {
console.log('error', error);
} else {
fs.writeFileSync(
`${process.cwd()}/${this.outputDir}/${this.title}.cpuprofile`,
result
);
}
});
}
}
}
export default Profiler;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment