Created
May 10, 2018 20:01
-
-
Save sverweij/0f288b760f5d1c5d7f0dfe19151238b1 to your computer and use it in GitHub Desktop.
Profile node code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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