Skip to content

Instantly share code, notes, and snippets.

@sverweij
Created May 10, 2018 20:01
Show Gist options
  • Save sverweij/0f288b760f5d1c5d7f0dfe19151238b1 to your computer and use it in GitHub Desktop.
Save sverweij/0f288b760f5d1c5d7f0dfe19151238b1 to your computer and use it in GitHub Desktop.
Profile node code
// source: https://developer.ibm.com/node/2016/01/06/tips-optimizing-slow-code-in-nodejs/
// the resulting logfile can be imported into google chrome's JavaScript profiler (which seems
// to be deprecated???)
const profiler = require('v8-profiler');
const fs = require('fs');
let profilerRunning = false;
function toggleProfiling () {
if (profilerRunning) {
const profile = profiler.stopProfiling();
// console.log('stopped profiling');
profile.export()
.pipe(fs.createWriteStream(`./myapp-${Date.now()}.cpuprofile`))
.once('error', profiler.deleteAllProfiles)
.once('finish', profiler.deleteAllProfiles);
profilerRunning = false;
return;
}
profiler.startProfiling();
profilerRunning = true;
// console.log('started profiling');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment