node/benchmark/es/error-stack.js
legendecas 803a7b2b23
src,lib: print source map error source on demand
The source context is not prepended to the value of the `stack` property
when the source map is not enabled. Rather than prepending the error
source context to the value of the `stack` property unconditionally,
this patch aligns the behavior and only prints the source context when
the error is not handled by userland (e.g. fatal errors).

Also, this patch fixes that when source-map support is enabled, the
error source context is not pointing to where the error was thrown.

PR-URL: https://github.com/nodejs/node/pull/43875
Fixes: https://github.com/nodejs/node/issues/43186
Fixes: https://github.com/nodejs/node/issues/41541
Reviewed-By: Ben Coe <bencoe@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2022-07-28 08:09:30 +08:00

35 lines
737 B
JavaScript

'use strict';
const common = require('../common.js');
const modPath = require.resolve('../fixtures/simple-error-stack.js');
const bench = common.createBenchmark(main, {
method: ['without-sourcemap', 'sourcemap'],
n: [1e5],
});
function runN(n) {
delete require.cache[modPath];
const mod = require(modPath);
bench.start();
for (let i = 0; i < n; i++) {
mod.simpleErrorStack();
}
bench.end(n);
}
function main({ n, method }) {
switch (method) {
case 'without-sourcemap':
process.setSourceMapsEnabled(false);
runN(n);
break;
case 'sourcemap':
process.setSourceMapsEnabled(true);
runN(n);
break;
default:
throw new Error(`Unexpected method "${method}"`);
}
}