node/benchmark/async_hooks/promises.js
legendecas b3b0ae565a benchmark: benchmarking impacts of async hooks on promises
PR-URL: https://github.com/nodejs/node/pull/31188
Refs: https://github.com/nodejs/diagnostics/issues/124
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
2020-01-08 06:18:30 -08:00

31 lines
608 B
JavaScript

'use strict';
const common = require('../common.js');
const { createHook } = require('async_hooks');
const bench = common.createBenchmark(main, {
n: [1e6],
asyncHooks: [
'enabled',
'disabled',
]
});
async function run(n) {
for (let i = 0; i < n; i++) {
await new Promise((resolve) => resolve())
.then(() => { throw new Error('foobar'); })
.catch((e) => e);
}
}
function main({ n, asyncHooks }) {
const hook = createHook({ promiseResolve() {} });
if (asyncHooks !== 'disabled') {
hook.enable();
}
bench.start();
run(n).then(() => {
bench.end(n);
});
}