Skip to content

Instantly share code, notes, and snippets.

@pragyandas
Created June 20, 2020 15:03
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 pragyandas/ca5282d8fd6137d1dd8d7ce3b4be58b7 to your computer and use it in GitHub Desktop.
Save pragyandas/ca5282d8fd6137d1dd8d7ce3b4be58b7 to your computer and use it in GitHub Desktop.
Promise performance using async and performance hooks
'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