Created
June 20, 2020 15:03
-
-
Save pragyandas/ca5282d8fd6137d1dd8d7ce3b4be58b7 to your computer and use it in GitHub Desktop.
Promise performance using async and performance hooks
This file contains 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
'use strict'; | |
const async_hooks = require('async_hooks'); | |
const { | |
performance, | |
PerformanceObserver | |
} = require('perf_hooks'); | |
const set = new Set(); | |
const hook = async_hooks.createHook({ | |
init(id, type, triggerAsyncId, resource) { | |
if (type === 'PROMISE') { | |
performance.mark(`${id}-Init`); | |
set.add(id); | |
} | |
}, | |
promiseResolve(id) { | |
if (set.has(id)) { | |
set.delete(id); | |
performance.mark(`${id}-Resolved`); | |
performance.measure(`Entry-${id}`, | |
`${id}-Init`, | |
`${id}-Resolved`); | |
} | |
} | |
}); | |
hook.enable(); | |
const obs = new PerformanceObserver((list, observer) => { | |
console.log(list.getEntries()); | |
}); | |
obs.observe({ entryTypes: ['measure'], buffered: true }); | |
async function execTimeout () { | |
const msg = await timesup(); | |
return msg; | |
}; | |
function timesup () { | |
return new Promise ((resolve) => { | |
setTimeout(() => {resolve('Hello');}, 1000); | |
}); | |
} | |
execTimeout(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment