From 197cabf7560a0b2d7c8caa9b7fb0b7dddf4a7a22 Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Mon, 28 Apr 2025 14:27:39 -0300 Subject: [PATCH 01/17] benchmark: add source map basic performance tests --- benchmark/source_map/source-map.js | 60 ++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 benchmark/source_map/source-map.js diff --git a/benchmark/source_map/source-map.js b/benchmark/source_map/source-map.js new file mode 100644 index 00000000000000..38f732a0ef3ca6 --- /dev/null +++ b/benchmark/source_map/source-map.js @@ -0,0 +1,60 @@ +'use strict'; + +const common = require('../common.js'); + +const options = { + flags: ['--expose-internals'], +}; + +const bench = common.createBenchmark( + main, + { + operation: ['parse', 'findEntry', 'findOrigin'], + n: [1e5], + }, + options +); + +function main({ operation, n }) { + const { SourceMap } = require('internal/source_map/source_map'); + + const samplePayload = { + version: 3, + file: 'out.js', + sourceRoot: '', + sources: ['foo.js', 'bar.js'], + sourcesContent: ['console.log("foo");', 'console.log("bar");'], + names: ['src', 'maps', 'are', 'fun'], + mappings: 'A,AAAB;;ABCDE;', + }; + const sourceMap = new SourceMap(samplePayload); + + switch (operation) { + case 'parse': + bench.start(); + for (let i = 0; i < n; i++) { + new SourceMap(samplePayload); + } + bench.end(n); + break; + + case 'findEntry': + bench.start(); + for (let i = 0; i < n; i++) { + sourceMap.findEntry(1, 1); + } + bench.end(n); + break; + + case 'findOrigin': + bench.start(); + for (let i = 0; i < n; i++) { + sourceMap.findOrigin(2, 5); + } + bench.end(n); + break; + + default: + throw new Error(`Unknown operation: ${operation}`); + } +} From b41dcedd538dc0edff984ca26f37032604ebb98e Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Mon, 28 Apr 2025 18:10:11 -0300 Subject: [PATCH 02/17] benchmark: expand operations and add performance tests for minified, sectioned, and large source maps --- benchmark/source_map/source-map.js | 68 +++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/benchmark/source_map/source-map.js b/benchmark/source_map/source-map.js index 38f732a0ef3ca6..8bd2758a874ee9 100644 --- a/benchmark/source_map/source-map.js +++ b/benchmark/source_map/source-map.js @@ -9,7 +9,14 @@ const options = { const bench = common.createBenchmark( main, { - operation: ['parse', 'findEntry', 'findOrigin'], + operation: [ + 'parse', + 'parse-minified', + 'parse-sectioned', + 'parse-large', + 'findEntry', + 'findOrigin', + ], n: [1e5], }, options @@ -23,10 +30,43 @@ function main({ operation, n }) { file: 'out.js', sourceRoot: '', sources: ['foo.js', 'bar.js'], - sourcesContent: ['console.log("foo");', 'console.log("bar");'], + sourcesContent: [null, null], names: ['src', 'maps', 'are', 'fun'], mappings: 'A,AAAB;;ABCDE;', }; + const minifiedPayload = { + version: 3, + file: 'add.min.js', + sourceRoot: '', + sources: ['add.ts'], + names: ['add', 'x', 'y'], + mappings: + 'AAAA,QAASA,EAAGC,EAAGC,CAClB,OAAOD,EAAGC,GACZC,QAAQC,IAAI,CAAC,CAAC,CAAC', + }; + const sectionedPayload = { + version: 3, + file: 'app.js', + sections: [ + { + offset: { line: 100, column: 10 }, + map: { + version: 3, + file: 'section.js', + sources: ['foo.js', 'bar.js'], + names: ['src', 'maps', 'are', 'fun'], + mappings: 'AAAA,E;;ABCDE;', + }, + }, + ], + }; + const largePayload = { + version: 3, + file: 'out.js', + sources: Array.from({ length: 1000 }, (_, i) => `source-${i}.js`), + names: Array.from({ length: 1000 }, (_, i) => `name-${i}`), + mappings: 'A,AAAB;;ABCDE;'.repeat(1000), + }; + const sourceMap = new SourceMap(samplePayload); switch (operation) { @@ -38,6 +78,30 @@ function main({ operation, n }) { bench.end(n); break; + case 'parse-minified': + bench.start(); + for (let i = 0; i < n; i++) { + new SourceMap(minifiedPayload); + } + bench.end(n); + break; + + case 'parse-sectioned': + bench.start(); + for (let i = 0; i < n; i++) { + new SourceMap(sectionedPayload); + } + bench.end(n); + break; + + case 'parse-large': + bench.start(); + for (let i = 0; i < n; i++) { + new SourceMap(largePayload); + } + bench.end(n); + break; + case 'findEntry': bench.start(); for (let i = 0; i < n; i++) { From 8c017ded12ee8a62653a3330630a6bc62f9367dd Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Tue, 29 Apr 2025 08:37:36 -0300 Subject: [PATCH 03/17] benchmark: add performance tests for findEntry and findOrigin operations on other source map types --- benchmark/source_map/source-map.js | 59 +++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/benchmark/source_map/source-map.js b/benchmark/source_map/source-map.js index 8bd2758a874ee9..43901f26e1f16e 100644 --- a/benchmark/source_map/source-map.js +++ b/benchmark/source_map/source-map.js @@ -15,11 +15,17 @@ const bench = common.createBenchmark( 'parse-sectioned', 'parse-large', 'findEntry', + 'findEntry-minified', + 'findEntry-sectioned', + 'findEntry-large', 'findOrigin', + 'findOrigin-minified', + 'findOrigin-sectioned', + 'findOrigin-large', ], n: [1e5], }, - options + options, ); function main({ operation, n }) { @@ -68,6 +74,9 @@ function main({ operation, n }) { }; const sourceMap = new SourceMap(samplePayload); + const minifiedSourceMap = new SourceMap(minifiedPayload); + const sectionedSourceMap = new SourceMap(sectionedPayload); + const largeSourceMap = new SourceMap(largePayload); switch (operation) { case 'parse': @@ -110,6 +119,30 @@ function main({ operation, n }) { bench.end(n); break; + case 'findEntry-minified': + bench.start(); + for (let i = 0; i < n; i++) { + minifiedSourceMap.findEntry(2, 2); + } + bench.end(n); + break; + + case 'findEntry-sectioned': + bench.start(); + for (let i = 0; i < n; i++) { + sectionedSourceMap.findEntry(3, 3); + } + bench.end(n); + break; + + case 'findEntry-large': + bench.start(); + for (let i = 0; i < n; i++) { + largeSourceMap.findEntry(10, 10); + } + bench.end(n); + break; + case 'findOrigin': bench.start(); for (let i = 0; i < n; i++) { @@ -118,6 +151,30 @@ function main({ operation, n }) { bench.end(n); break; + case 'findOrigin-minified': + bench.start(); + for (let i = 0; i < n; i++) { + minifiedSourceMap.findOrigin(3, 6); + } + bench.end(n); + break; + + case 'findOrigin-sectioned': + bench.start(); + for (let i = 0; i < n; i++) { + sectionedSourceMap.findOrigin(4, 7); + } + bench.end(n); + break; + + case 'findOrigin-large': + bench.start(); + for (let i = 0; i < n; i++) { + largeSourceMap.findOrigin(12, 15); + } + bench.end(n); + break; + default: throw new Error(`Unknown operation: ${operation}`); } From 338bf9189acdb2a1b6eada86848ecb9190670dbb Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Tue, 29 Apr 2025 11:56:01 -0300 Subject: [PATCH 04/17] benchmark: add initial performance benchmark for findSourceMap operation --- benchmark/source_map/source-map-cache.js | 48 ++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 benchmark/source_map/source-map-cache.js diff --git a/benchmark/source_map/source-map-cache.js b/benchmark/source_map/source-map-cache.js new file mode 100644 index 00000000000000..f59440d73f0afb --- /dev/null +++ b/benchmark/source_map/source-map-cache.js @@ -0,0 +1,48 @@ +'use strict'; + +const common = require('../common.js'); +const fs = require('fs'); +const path = require('path'); + +const options = { + flags: ['--expose-internals'], +}; + +const bench = common.createBenchmark( + main, + { + operation: ['findSourceMap-valid'], + n: [1e5], + }, + options, +); + +function main({ operation, n }) { + const { + findSourceMap, + maybeCacheSourceMap, + } = require('internal/source_map/source_map_cache'); + + const validFileName = path.resolve( + __dirname, + '../../test/fixtures/test-runner/source-maps/line-lengths/index.js', + ); + const validMapFile = path.resolve(validFileName + '.map'); + const validFileContent = fs.readFileSync(validFileName, 'utf8'); + fs.readFileSync(validMapFile, 'utf8'); + + maybeCacheSourceMap(validFileName, validFileContent, null, false); + + switch (operation) { + case 'findSourceMap-valid': + bench.start(); + for (let i = 0; i < n; i++) { + findSourceMap(validFileName); + } + bench.end(n); + break; + + default: + throw new Error(`Unknown operation: ${operation}`); + } +} From c50128d6fc1a2a6ceb4ffd91acebc57fc7aa7eda Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Tue, 29 Apr 2025 18:15:55 -0300 Subject: [PATCH 05/17] benchmark: add performance tests for findSourceMap with invalid and missing cases --- benchmark/source_map/source-map-cache.js | 35 ++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/benchmark/source_map/source-map-cache.js b/benchmark/source_map/source-map-cache.js index f59440d73f0afb..55ecf0773aa424 100644 --- a/benchmark/source_map/source-map-cache.js +++ b/benchmark/source_map/source-map-cache.js @@ -11,10 +11,14 @@ const options = { const bench = common.createBenchmark( main, { - operation: ['findSourceMap-valid'], + operation: [ + 'findSourceMap-valid', + 'findSourceMap-invalid', + 'findSourceMap-missing', + ], n: [1e5], }, - options, + options ); function main({ operation, n }) { @@ -31,7 +35,18 @@ function main({ operation, n }) { const validFileContent = fs.readFileSync(validFileName, 'utf8'); fs.readFileSync(validMapFile, 'utf8'); + const invalidFileName = path.resolve( + __dirname, + '../../test/fixtures/test-runner/source-maps/invalid-json/index.js', + ); + const invalidMapFile = path.resolve(invalidFileName + '.map'); + const invalidFileContent = fs.readFileSync(invalidFileName, 'utf8'); + fs.readFileSync(invalidMapFile, 'utf8'); + + const missingSourceURL = 'missing-source-url.js'; + maybeCacheSourceMap(validFileName, validFileContent, null, false); + maybeCacheSourceMap(invalidFileName, invalidFileContent, null, false); switch (operation) { case 'findSourceMap-valid': @@ -42,6 +57,22 @@ function main({ operation, n }) { bench.end(n); break; + case 'findSourceMap-invalid': + bench.start(); + for (let i = 0; i < n; i++) { + findSourceMap(invalidFileName); + } + bench.end(n); + break; + + case 'findSourceMap-missing': + bench.start(); + for (let i = 0; i < n; i++) { + findSourceMap(missingSourceURL); + } + bench.end(n); + break; + default: throw new Error(`Unknown operation: ${operation}`); } From a0bcf810047b6a18ccb61f7886d083dd5c4c9427 Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Tue, 29 Apr 2025 18:35:45 -0300 Subject: [PATCH 06/17] benchmark: add performance test for findSourceMap with generated source --- benchmark/source_map/source-map-cache.js | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/benchmark/source_map/source-map-cache.js b/benchmark/source_map/source-map-cache.js index 55ecf0773aa424..59f7d3489bff86 100644 --- a/benchmark/source_map/source-map-cache.js +++ b/benchmark/source_map/source-map-cache.js @@ -15,10 +15,11 @@ const bench = common.createBenchmark( 'findSourceMap-valid', 'findSourceMap-invalid', 'findSourceMap-missing', + 'findSourceMap-generated-source', ], n: [1e5], }, - options + options, ); function main({ operation, n }) { @@ -45,8 +46,20 @@ function main({ operation, n }) { const missingSourceURL = 'missing-source-url.js'; + const generatedSourceFileName = 'generated-source.js'; + const generatedSourceContent = eval(` + function hello() { + console.log('Hello, World!'); + } + // # sourceMappingURL=${generatedSourceFileName} + `); + maybeCacheSourceMap(validFileName, validFileContent, null, false); maybeCacheSourceMap(invalidFileName, invalidFileContent, null, false); + maybeCacheSourceMap(generatedSourceFileName, generatedSourceContent, null, true, + `/${generatedSourceFileName}`, + `${generatedSourceFileName}.map`, + ); switch (operation) { case 'findSourceMap-valid': @@ -73,6 +86,14 @@ function main({ operation, n }) { bench.end(n); break; + case 'findSourceMap-generated-source': + bench.start(); + for (let i = 0; i < n; i++) { + findSourceMap(generatedSourceFileName); + } + bench.end(n); + break; + default: throw new Error(`Unknown operation: ${operation}`); } From bda2344fd98562062e035180b40c3cc964ea0593 Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Wed, 30 Apr 2025 16:07:54 -0300 Subject: [PATCH 07/17] benchmark: add performance benchmark for prepareStackTraceWithSourceMaps --- benchmark/es/error-stack.js | 2 +- benchmark/source_map/prepare-stack-trace.js | 61 +++++++++++++++++++ benchmark/source_map/source-map-cache.js | 5 +- .../writing-and-running-benchmarks.md | 2 +- 4 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 benchmark/source_map/prepare-stack-trace.js diff --git a/benchmark/es/error-stack.js b/benchmark/es/error-stack.js index 2cc3321640c5cd..3b373dcdae63c8 100644 --- a/benchmark/es/error-stack.js +++ b/benchmark/es/error-stack.js @@ -10,7 +10,7 @@ const bench = common.createBenchmark(main, { 'without-sourcemap', 'sourcemap', 'node-modules-without-sourcemap', - 'node-modules-sourcemap'], + 'node-module-sourcemap'], n: [1e5], }); diff --git a/benchmark/source_map/prepare-stack-trace.js b/benchmark/source_map/prepare-stack-trace.js new file mode 100644 index 00000000000000..f226406fbcf562 --- /dev/null +++ b/benchmark/source_map/prepare-stack-trace.js @@ -0,0 +1,61 @@ +'use strict'; + +const common = require('../common.js'); +const kIsNodeError = Symbol('kIsNodeError'); + +const options = { + flags: ['--expose-internals'], +}; + +const bench = common.createBenchmark( + main, + { + operation: ['node-error-callsite', 'non-node-error-callsite'], + n: [1e5], + }, + options, +); + +function main({ operation, n }) { + const { prepareStackTraceWithSourceMaps } = require('internal/source_map/prepare_stack_trace'); + + const nodeError = new Error('Simulated Node.js error'); + nodeError.name = 'NodeError'; + nodeError.code = 'ERR_SIMULATED'; + nodeError[kIsNodeError] = true; + + const nodeStackTrace = Array.from({ length: 10 }, (_, i) => ({ + getFileName: () => `file${i}.js`, + getEvalOrigin: () => `eval at (eval${i}.js:1:1)`, + getLineNumber: () => i + 1, + getColumnNumber: () => 1, + getFunctionName: () => `func${i}`, + isAsync: () => false, + isConstructor: () => false, + getTypeName: () => null, + })); + + const nonNodeError = new Error('Simulated non-Node.js error'); + nonNodeError.name = 'NonNodeError'; + + switch (operation) { + case 'node-error-callsite': + bench.start(); + for (let i = 0; i < n; i++) { + prepareStackTraceWithSourceMaps(nodeError, nodeStackTrace); + } + bench.end(n); + break; + + case 'non-node-error-callsite': + bench.start(); + for (let i = 0; i < n; i++) { + prepareStackTraceWithSourceMaps(nonNodeError, nodeStackTrace); + } + bench.end(n); + break; + + default: + throw new Error(`Unknown operation: ${operation}`); + } +} diff --git a/benchmark/source_map/source-map-cache.js b/benchmark/source_map/source-map-cache.js index 59f7d3489bff86..4591ad13bb053a 100644 --- a/benchmark/source_map/source-map-cache.js +++ b/benchmark/source_map/source-map-cache.js @@ -57,9 +57,8 @@ function main({ operation, n }) { maybeCacheSourceMap(validFileName, validFileContent, null, false); maybeCacheSourceMap(invalidFileName, invalidFileContent, null, false); maybeCacheSourceMap(generatedSourceFileName, generatedSourceContent, null, true, - `/${generatedSourceFileName}`, - `${generatedSourceFileName}.map`, - ); + `/${generatedSourceFileName}`, + `${generatedSourceFileName}.map`); switch (operation) { case 'findSourceMap-valid': diff --git a/doc/contributing/writing-and-running-benchmarks.md b/doc/contributing/writing-and-running-benchmarks.md index 2664116aefc4b3..efe68920ef0f03 100644 --- a/doc/contributing/writing-and-running-benchmarks.md +++ b/doc/contributing/writing-and-running-benchmarks.md @@ -101,7 +101,7 @@ benchmarks. This increases the likelihood of each benchmark achieving peak perfo according to the hardware. Therefore, run: ```console -$ ./benchmark/cpu.sh fast +$ ./benchmarks/cpu.sh fast ``` ### Running individual benchmarks From ea724fcc7374f2791ad070d03005edfc539d9117 Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Fri, 2 May 2025 19:33:55 -0300 Subject: [PATCH 08/17] benchmark: refactor payload handling and avoid V8 deadcode elimination --- benchmark/source_map/source-map.js | 101 ++++++++++++----------------- 1 file changed, 42 insertions(+), 59 deletions(-) diff --git a/benchmark/source_map/source-map.js b/benchmark/source_map/source-map.js index 43901f26e1f16e..c19c6cb389c541 100644 --- a/benchmark/source_map/source-map.js +++ b/benchmark/source_map/source-map.js @@ -1,6 +1,9 @@ 'use strict'; const common = require('../common.js'); +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); const options = { flags: ['--expose-internals'], @@ -31,58 +34,37 @@ const bench = common.createBenchmark( function main({ operation, n }) { const { SourceMap } = require('internal/source_map/source_map'); - const samplePayload = { - version: 3, - file: 'out.js', - sourceRoot: '', - sources: ['foo.js', 'bar.js'], - sourcesContent: [null, null], - names: ['src', 'maps', 'are', 'fun'], - mappings: 'A,AAAB;;ABCDE;', - }; - const minifiedPayload = { - version: 3, - file: 'add.min.js', - sourceRoot: '', - sources: ['add.ts'], - names: ['add', 'x', 'y'], - mappings: - 'AAAA,QAASA,EAAGC,EAAGC,CAClB,OAAOD,EAAGC,GACZC,QAAQC,IAAI,CAAC,CAAC,CAAC', - }; - const sectionedPayload = { - version: 3, - file: 'app.js', - sections: [ - { - offset: { line: 100, column: 10 }, - map: { - version: 3, - file: 'section.js', - sources: ['foo.js', 'bar.js'], - names: ['src', 'maps', 'are', 'fun'], - mappings: 'AAAA,E;;ABCDE;', - }, - }, - ], - }; - const largePayload = { - version: 3, - file: 'out.js', - sources: Array.from({ length: 1000 }, (_, i) => `source-${i}.js`), - names: Array.from({ length: 1000 }, (_, i) => `name-${i}`), - mappings: 'A,AAAB;;ABCDE;'.repeat(1000), - }; - - const sourceMap = new SourceMap(samplePayload); - const minifiedSourceMap = new SourceMap(minifiedPayload); - const sectionedSourceMap = new SourceMap(sectionedPayload); - const largeSourceMap = new SourceMap(largePayload); - + const samplePayload = JSON.parse( + fs.readFileSync( + path.resolve(__dirname, '../../test/fixtures/source-map/no-source.js.map'), + 'utf8', + ), + ); + const minifiedPayload = JSON.parse( + fs.readFileSync( + path.resolve(__dirname, '../../test/fixtures/source-map/enclosing-call-site.js.map'), + 'utf8', + ), + ); + const sectionedPayload = JSON.parse( + fs.readFileSync( + path.resolve(__dirname, '../../test/fixtures/source-map/disk-index.map'), + 'utf8', + ), + ); + const largePayload = JSON.parse( + fs.readFileSync( + path.resolve(__dirname, '../../test/fixtures/test-runner/source-maps/line-lengths/index.js.map'), + 'utf8', + ), + ); + + let sourceMap; switch (operation) { case 'parse': bench.start(); for (let i = 0; i < n; i++) { - new SourceMap(samplePayload); + sourceMap = new SourceMap(samplePayload); } bench.end(n); break; @@ -90,7 +72,7 @@ function main({ operation, n }) { case 'parse-minified': bench.start(); for (let i = 0; i < n; i++) { - new SourceMap(minifiedPayload); + sourceMap = new SourceMap(minifiedPayload); } bench.end(n); break; @@ -98,7 +80,7 @@ function main({ operation, n }) { case 'parse-sectioned': bench.start(); for (let i = 0; i < n; i++) { - new SourceMap(sectionedPayload); + sourceMap = new SourceMap(sectionedPayload); } bench.end(n); break; @@ -106,7 +88,7 @@ function main({ operation, n }) { case 'parse-large': bench.start(); for (let i = 0; i < n; i++) { - new SourceMap(largePayload); + sourceMap = new SourceMap(largePayload); } bench.end(n); break; @@ -114,7 +96,7 @@ function main({ operation, n }) { case 'findEntry': bench.start(); for (let i = 0; i < n; i++) { - sourceMap.findEntry(1, 1); + sourceMap = new SourceMap(samplePayload).findEntry(i, i); } bench.end(n); break; @@ -122,7 +104,7 @@ function main({ operation, n }) { case 'findEntry-minified': bench.start(); for (let i = 0; i < n; i++) { - minifiedSourceMap.findEntry(2, 2); + sourceMap = new SourceMap(minifiedPayload).findEntry(i, i); } bench.end(n); break; @@ -130,7 +112,7 @@ function main({ operation, n }) { case 'findEntry-sectioned': bench.start(); for (let i = 0; i < n; i++) { - sectionedSourceMap.findEntry(3, 3); + sourceMap = new SourceMap(sectionedPayload).findEntry(i, i); } bench.end(n); break; @@ -138,7 +120,7 @@ function main({ operation, n }) { case 'findEntry-large': bench.start(); for (let i = 0; i < n; i++) { - largeSourceMap.findEntry(10, 10); + sourceMap = new SourceMap(largePayload).findEntry(i, i); } bench.end(n); break; @@ -146,7 +128,7 @@ function main({ operation, n }) { case 'findOrigin': bench.start(); for (let i = 0; i < n; i++) { - sourceMap.findOrigin(2, 5); + sourceMap = new SourceMap(samplePayload).findOrigin(i, i); } bench.end(n); break; @@ -154,7 +136,7 @@ function main({ operation, n }) { case 'findOrigin-minified': bench.start(); for (let i = 0; i < n; i++) { - minifiedSourceMap.findOrigin(3, 6); + sourceMap = new SourceMap(minifiedPayload).findOrigin(i, i); } bench.end(n); break; @@ -162,7 +144,7 @@ function main({ operation, n }) { case 'findOrigin-sectioned': bench.start(); for (let i = 0; i < n; i++) { - sectionedSourceMap.findOrigin(4, 7); + sourceMap = new SourceMap(sectionedPayload).findOrigin(i, i); } bench.end(n); break; @@ -170,7 +152,7 @@ function main({ operation, n }) { case 'findOrigin-large': bench.start(); for (let i = 0; i < n; i++) { - largeSourceMap.findOrigin(12, 15); + sourceMap = new SourceMap(largePayload).findOrigin(i, i); } bench.end(n); break; @@ -178,4 +160,5 @@ function main({ operation, n }) { default: throw new Error(`Unknown operation: ${operation}`); } + assert.ok(sourceMap); } From acae481a8269f1490f5a35b24a74dca66f0c6db1 Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Sat, 3 May 2025 14:56:29 -0300 Subject: [PATCH 09/17] benchmark: refactor payload handling and avoid v8 deadcode elimination for prepare stack trace --- benchmark/source_map/prepare-stack-trace.js | 37 +++++++++------------ 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/benchmark/source_map/prepare-stack-trace.js b/benchmark/source_map/prepare-stack-trace.js index f226406fbcf562..ae58d4a9f4810d 100644 --- a/benchmark/source_map/prepare-stack-trace.js +++ b/benchmark/source_map/prepare-stack-trace.js @@ -1,6 +1,7 @@ 'use strict'; const common = require('../common.js'); +const assert = require('assert'); const kIsNodeError = Symbol('kIsNodeError'); const options = { @@ -10,7 +11,7 @@ const options = { const bench = common.createBenchmark( main, { - operation: ['node-error-callsite', 'non-node-error-callsite'], + operation: ['node-error', 'non-node-error'], n: [1e5], }, options, @@ -18,39 +19,32 @@ const bench = common.createBenchmark( function main({ operation, n }) { const { prepareStackTraceWithSourceMaps } = require('internal/source_map/prepare_stack_trace'); + const { + ERR_ASSERTION, + } = require('internal/errors').codes; - const nodeError = new Error('Simulated Node.js error'); - nodeError.name = 'NodeError'; - nodeError.code = 'ERR_SIMULATED'; + const nodeError = new ERR_ASSERTION('Node error'); nodeError[kIsNodeError] = true; + // Stacktrace is not formatted until it is accessed + const nodeErrorStackTrace = nodeError.stack.split('\n').slice(1); - const nodeStackTrace = Array.from({ length: 10 }, (_, i) => ({ - getFileName: () => `file${i}.js`, - getEvalOrigin: () => `eval at (eval${i}.js:1:1)`, - getLineNumber: () => i + 1, - getColumnNumber: () => 1, - getFunctionName: () => `func${i}`, - isAsync: () => false, - isConstructor: () => false, - getTypeName: () => null, - })); - - const nonNodeError = new Error('Simulated non-Node.js error'); - nonNodeError.name = 'NonNodeError'; + const nonNodeError = new ERR_ASSERTION('non Node error'); + const nonNodeErrorStackTrace = nonNodeError.stack.split('\n').slice(1); + let preparedStackTrace; switch (operation) { - case 'node-error-callsite': + case 'node-error': bench.start(); for (let i = 0; i < n; i++) { - prepareStackTraceWithSourceMaps(nodeError, nodeStackTrace); + preparedStackTrace = prepareStackTraceWithSourceMaps(nodeError, nodeErrorStackTrace); } bench.end(n); break; - case 'non-node-error-callsite': + case 'non-node-error': bench.start(); for (let i = 0; i < n; i++) { - prepareStackTraceWithSourceMaps(nonNodeError, nodeStackTrace); + preparedStackTrace = prepareStackTraceWithSourceMaps(nonNodeError, nonNodeErrorStackTrace); } bench.end(n); break; @@ -58,4 +52,5 @@ function main({ operation, n }) { default: throw new Error(`Unknown operation: ${operation}`); } + assert.ok(preparedStackTrace); } From dbceb0e8a6e0773da978bd6f00ea1ed9daf42b86 Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Sat, 3 May 2025 17:52:43 -0300 Subject: [PATCH 10/17] benchmark: refactor payload handling and avoid v8 deadcode elimination for source map cache --- benchmark/source_map/source-map-cache.js | 66 +++++++----------------- 1 file changed, 20 insertions(+), 46 deletions(-) diff --git a/benchmark/source_map/source-map-cache.js b/benchmark/source_map/source-map-cache.js index 4591ad13bb053a..9827072e1b69e0 100644 --- a/benchmark/source_map/source-map-cache.js +++ b/benchmark/source_map/source-map-cache.js @@ -1,6 +1,7 @@ 'use strict'; const common = require('../common.js'); +const assert = require('assert'); const fs = require('fs'); const path = require('path'); @@ -13,82 +14,54 @@ const bench = common.createBenchmark( { operation: [ 'findSourceMap-valid', - 'findSourceMap-invalid', - 'findSourceMap-missing', 'findSourceMap-generated-source', ], - n: [1e5], + n: [10], }, options, ); function main({ operation, n }) { const { - findSourceMap, + setSourceMapsSupport, maybeCacheSourceMap, + findSourceMap, } = require('internal/source_map/source_map_cache'); + setSourceMapsSupport(true); const validFileName = path.resolve( __dirname, '../../test/fixtures/test-runner/source-maps/line-lengths/index.js', ); const validMapFile = path.resolve(validFileName + '.map'); const validFileContent = fs.readFileSync(validFileName, 'utf8'); - fs.readFileSync(validMapFile, 'utf8'); - - const invalidFileName = path.resolve( - __dirname, - '../../test/fixtures/test-runner/source-maps/invalid-json/index.js', - ); - const invalidMapFile = path.resolve(invalidFileName + '.map'); - const invalidFileContent = fs.readFileSync(invalidFileName, 'utf8'); - fs.readFileSync(invalidMapFile, 'utf8'); - - const missingSourceURL = 'missing-source-url.js'; - - const generatedSourceFileName = 'generated-source.js'; - const generatedSourceContent = eval(` - function hello() { - console.log('Hello, World!'); - } - // # sourceMappingURL=${generatedSourceFileName} - `); - - maybeCacheSourceMap(validFileName, validFileContent, null, false); - maybeCacheSourceMap(invalidFileName, invalidFileContent, null, false); - maybeCacheSourceMap(generatedSourceFileName, generatedSourceContent, null, true, - `/${generatedSourceFileName}`, - `${generatedSourceFileName}.map`); + const fakeModule = { filename: validFileName }; + let sourceMap; switch (operation) { case 'findSourceMap-valid': bench.start(); - for (let i = 0; i < n; i++) { - findSourceMap(validFileName); - } - bench.end(n); - break; - - case 'findSourceMap-invalid': - bench.start(); - for (let i = 0; i < n; i++) { - findSourceMap(invalidFileName); - } - bench.end(n); - break; + maybeCacheSourceMap(validFileName, validFileContent, fakeModule, false); - case 'findSourceMap-missing': - bench.start(); for (let i = 0; i < n; i++) { - findSourceMap(missingSourceURL); + sourceMap = findSourceMap(validFileName); } bench.end(n); break; case 'findSourceMap-generated-source': bench.start(); + maybeCacheSourceMap( + validFileName, + validFileContent, + fakeModule, + true, + validFileName, + validMapFile, + ); + for (let i = 0; i < n; i++) { - findSourceMap(generatedSourceFileName); + sourceMap = findSourceMap(validFileName); } bench.end(n); break; @@ -96,4 +69,5 @@ function main({ operation, n }) { default: throw new Error(`Unknown operation: ${operation}`); } + assert.ok(sourceMap); } From 7fe30ea08d945d664f61f23661b2fe457b5a219e Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Sat, 3 May 2025 17:55:15 -0300 Subject: [PATCH 11/17] benchmark: increase iteration count for source map cache --- benchmark/source_map/source-map-cache.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/source_map/source-map-cache.js b/benchmark/source_map/source-map-cache.js index 9827072e1b69e0..414a7c567e0cd2 100644 --- a/benchmark/source_map/source-map-cache.js +++ b/benchmark/source_map/source-map-cache.js @@ -16,7 +16,7 @@ const bench = common.createBenchmark( 'findSourceMap-valid', 'findSourceMap-generated-source', ], - n: [10], + n: [1e5], }, options, ); From 4ed4f78e2b21b24908d1de9e4aa4b5f35412a6c7 Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Sun, 4 May 2025 07:34:24 -0300 Subject: [PATCH 12/17] test: add test of source map benchmark --- test/benchmark/test-benchmark-source-map.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 test/benchmark/test-benchmark-source-map.js diff --git a/test/benchmark/test-benchmark-source-map.js b/test/benchmark/test-benchmark-source-map.js new file mode 100644 index 00000000000000..c4d58b4bdff62c --- /dev/null +++ b/test/benchmark/test-benchmark-source-map.js @@ -0,0 +1,7 @@ +'use strict'; + +require('../common'); + +const runBenchmark = require('../common/benchmark'); + +runBenchmark('source_map', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 }); From 7d105d78ffb7022fa94af7f2a557eb337e05a5cc Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Thu, 5 Jun 2025 10:57:12 -0300 Subject: [PATCH 13/17] benchmark: moving construction from bench scope for find methods --- benchmark/es/error-stack.js | 2 +- benchmark/source_map/source-map.js | 25 +++++++++++++------ .../writing-and-running-benchmarks.md | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/benchmark/es/error-stack.js b/benchmark/es/error-stack.js index 3b373dcdae63c8..2cc3321640c5cd 100644 --- a/benchmark/es/error-stack.js +++ b/benchmark/es/error-stack.js @@ -10,7 +10,7 @@ const bench = common.createBenchmark(main, { 'without-sourcemap', 'sourcemap', 'node-modules-without-sourcemap', - 'node-module-sourcemap'], + 'node-modules-sourcemap'], n: [1e5], }); diff --git a/benchmark/source_map/source-map.js b/benchmark/source_map/source-map.js index c19c6cb389c541..e6e43d2a2ebccf 100644 --- a/benchmark/source_map/source-map.js +++ b/benchmark/source_map/source-map.js @@ -60,6 +60,7 @@ function main({ operation, n }) { ); let sourceMap; + let instance; switch (operation) { case 'parse': bench.start(); @@ -94,65 +95,73 @@ function main({ operation, n }) { break; case 'findEntry': + instance = new SourceMap(samplePayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = new SourceMap(samplePayload).findEntry(i, i); + sourceMap = instance.findEntry(i, i); } bench.end(n); break; case 'findEntry-minified': + instance = new SourceMap(minifiedPayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = new SourceMap(minifiedPayload).findEntry(i, i); + sourceMap = instance.findEntry(i, i); } bench.end(n); break; case 'findEntry-sectioned': + instance = new SourceMap(sectionedPayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = new SourceMap(sectionedPayload).findEntry(i, i); + sourceMap = instance.findEntry(i, i); } bench.end(n); break; case 'findEntry-large': + instance = new SourceMap(largePayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = new SourceMap(largePayload).findEntry(i, i); + sourceMap = instance.findEntry(i, i); } bench.end(n); break; case 'findOrigin': + instance = new SourceMap(samplePayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = new SourceMap(samplePayload).findOrigin(i, i); + sourceMap = instance.findOrigin(i, i); } bench.end(n); break; case 'findOrigin-minified': + instance = new SourceMap(minifiedPayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = new SourceMap(minifiedPayload).findOrigin(i, i); + sourceMap = instance.findOrigin(i, i); } bench.end(n); break; case 'findOrigin-sectioned': + instance = new SourceMap(sectionedPayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = new SourceMap(sectionedPayload).findOrigin(i, i); + sourceMap = instance.findOrigin(i, i); } bench.end(n); break; case 'findOrigin-large': + instance = new SourceMap(largePayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = new SourceMap(largePayload).findOrigin(i, i); + sourceMap = instance.findOrigin(i, i); } bench.end(n); break; diff --git a/doc/contributing/writing-and-running-benchmarks.md b/doc/contributing/writing-and-running-benchmarks.md index efe68920ef0f03..2664116aefc4b3 100644 --- a/doc/contributing/writing-and-running-benchmarks.md +++ b/doc/contributing/writing-and-running-benchmarks.md @@ -101,7 +101,7 @@ benchmarks. This increases the likelihood of each benchmark achieving peak perfo according to the hardware. Therefore, run: ```console -$ ./benchmarks/cpu.sh fast +$ ./benchmark/cpu.sh fast ``` ### Running individual benchmarks From 421ae121f07819e6b844478228c160c6cb9367f7 Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Thu, 5 Jun 2025 15:51:07 -0300 Subject: [PATCH 14/17] benchmark: reduce iteration count and remove instance for find operations --- benchmark/source_map/source-map.js | 35 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/benchmark/source_map/source-map.js b/benchmark/source_map/source-map.js index e6e43d2a2ebccf..be7e265ef1a96f 100644 --- a/benchmark/source_map/source-map.js +++ b/benchmark/source_map/source-map.js @@ -26,7 +26,7 @@ const bench = common.createBenchmark( 'findOrigin-sectioned', 'findOrigin-large', ], - n: [1e5], + n: [10], }, options, ); @@ -60,7 +60,6 @@ function main({ operation, n }) { ); let sourceMap; - let instance; switch (operation) { case 'parse': bench.start(); @@ -95,73 +94,73 @@ function main({ operation, n }) { break; case 'findEntry': - instance = new SourceMap(samplePayload); + sourceMap = new SourceMap(samplePayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = instance.findEntry(i, i); + sourceMap.findEntry(i, i); } bench.end(n); break; case 'findEntry-minified': - instance = new SourceMap(minifiedPayload); + sourceMap = new SourceMap(minifiedPayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = instance.findEntry(i, i); + sourceMap.findEntry(i, i); } bench.end(n); break; case 'findEntry-sectioned': - instance = new SourceMap(sectionedPayload); + sourceMap = new SourceMap(sectionedPayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = instance.findEntry(i, i); + sourceMap.findEntry(i, i); } bench.end(n); break; case 'findEntry-large': - instance = new SourceMap(largePayload); + sourceMap = new SourceMap(largePayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = instance.findEntry(i, i); + sourceMap.findEntry(i, i); } bench.end(n); break; case 'findOrigin': - instance = new SourceMap(samplePayload); + sourceMap = new SourceMap(samplePayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = instance.findOrigin(i, i); + sourceMap.findOrigin(i, i); } bench.end(n); break; case 'findOrigin-minified': - instance = new SourceMap(minifiedPayload); + sourceMap = new SourceMap(minifiedPayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = instance.findOrigin(i, i); + sourceMap.findOrigin(i, i); } bench.end(n); break; case 'findOrigin-sectioned': - instance = new SourceMap(sectionedPayload); + sourceMap = new SourceMap(sectionedPayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = instance.findOrigin(i, i); + sourceMap.findOrigin(i, i); } bench.end(n); break; case 'findOrigin-large': - instance = new SourceMap(largePayload); + sourceMap = new SourceMap(largePayload); bench.start(); for (let i = 0; i < n; i++) { - sourceMap = instance.findOrigin(i, i); + sourceMap.findOrigin(i, i); } bench.end(n); break; From bccecc21b7c2acb5cb55ad703f0d89f480d4c634 Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Thu, 5 Jun 2025 19:37:29 -0300 Subject: [PATCH 15/17] benchmark: add valid map file to maybeCacheSourceMap --- benchmark/source_map/source-map-cache.js | 2 +- benchmark/source_map/source-map.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/benchmark/source_map/source-map-cache.js b/benchmark/source_map/source-map-cache.js index 414a7c567e0cd2..5d64bf4e37f665 100644 --- a/benchmark/source_map/source-map-cache.js +++ b/benchmark/source_map/source-map-cache.js @@ -41,7 +41,7 @@ function main({ operation, n }) { switch (operation) { case 'findSourceMap-valid': bench.start(); - maybeCacheSourceMap(validFileName, validFileContent, fakeModule, false); + maybeCacheSourceMap(validFileName, validFileContent, fakeModule, false, undefined, validMapFile); for (let i = 0; i < n; i++) { sourceMap = findSourceMap(validFileName); diff --git a/benchmark/source_map/source-map.js b/benchmark/source_map/source-map.js index be7e265ef1a96f..aed873b676ebbb 100644 --- a/benchmark/source_map/source-map.js +++ b/benchmark/source_map/source-map.js @@ -26,7 +26,7 @@ const bench = common.createBenchmark( 'findOrigin-sectioned', 'findOrigin-large', ], - n: [10], + n: [1e5], }, options, ); From d056eacd83e7415bf21dcfe9fe5db557efc453f4 Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Fri, 6 Jun 2025 18:03:15 -0300 Subject: [PATCH 16/17] benchmark:fix module imports and remove large payload cases --- benchmark/source_map/prepare-stack-trace.js | 34 ++++++++++--------- benchmark/source_map/source-map-cache.js | 6 ++-- benchmark/source_map/source-map.js | 37 +-------------------- 3 files changed, 22 insertions(+), 55 deletions(-) diff --git a/benchmark/source_map/prepare-stack-trace.js b/benchmark/source_map/prepare-stack-trace.js index ae58d4a9f4810d..18acd2d1c2fd06 100644 --- a/benchmark/source_map/prepare-stack-trace.js +++ b/benchmark/source_map/prepare-stack-trace.js @@ -2,7 +2,6 @@ const common = require('../common.js'); const assert = require('assert'); -const kIsNodeError = Symbol('kIsNodeError'); const options = { flags: ['--expose-internals'], @@ -11,36 +10,39 @@ const options = { const bench = common.createBenchmark( main, { - operation: ['node-error', 'non-node-error'], + operation: ['non-node-error'], n: [1e5], }, options, ); +function ErrorGetCallSites() { + const originalStackFormatter = Error.prepareStackTrace; + Error.prepareStackTrace = (_err, stack) => { + if (stack && stack.length > 1) { + // Remove node:util + return stack.slice(1); + } + return stack; + }; + const err = new Error(); + // With the V8 Error API, the stack is not formatted until it is accessed + err.stack; // eslint-disable-line no-unused-expressions + Error.prepareStackTrace = originalStackFormatter; + return err.stack; +} + function main({ operation, n }) { const { prepareStackTraceWithSourceMaps } = require('internal/source_map/prepare_stack_trace'); const { ERR_ASSERTION, } = require('internal/errors').codes; - const nodeError = new ERR_ASSERTION('Node error'); - nodeError[kIsNodeError] = true; - // Stacktrace is not formatted until it is accessed - const nodeErrorStackTrace = nodeError.stack.split('\n').slice(1); - const nonNodeError = new ERR_ASSERTION('non Node error'); - const nonNodeErrorStackTrace = nonNodeError.stack.split('\n').slice(1); + const nonNodeErrorStackTrace = ErrorGetCallSites(); let preparedStackTrace; switch (operation) { - case 'node-error': - bench.start(); - for (let i = 0; i < n; i++) { - preparedStackTrace = prepareStackTraceWithSourceMaps(nodeError, nodeErrorStackTrace); - } - bench.end(n); - break; - case 'non-node-error': bench.start(); for (let i = 0; i < n; i++) { diff --git a/benchmark/source_map/source-map-cache.js b/benchmark/source_map/source-map-cache.js index 5d64bf4e37f665..210c3a437cc0ea 100644 --- a/benchmark/source_map/source-map-cache.js +++ b/benchmark/source_map/source-map-cache.js @@ -25,8 +25,8 @@ function main({ operation, n }) { const { setSourceMapsSupport, maybeCacheSourceMap, - findSourceMap, } = require('internal/source_map/source_map_cache'); + const { findSourceMap } = require('node:module'); setSourceMapsSupport(true); const validFileName = path.resolve( @@ -40,8 +40,8 @@ function main({ operation, n }) { let sourceMap; switch (operation) { case 'findSourceMap-valid': - bench.start(); maybeCacheSourceMap(validFileName, validFileContent, fakeModule, false, undefined, validMapFile); + bench.start(); for (let i = 0; i < n; i++) { sourceMap = findSourceMap(validFileName); @@ -50,7 +50,6 @@ function main({ operation, n }) { break; case 'findSourceMap-generated-source': - bench.start(); maybeCacheSourceMap( validFileName, validFileContent, @@ -59,6 +58,7 @@ function main({ operation, n }) { validFileName, validMapFile, ); + bench.start(); for (let i = 0; i < n; i++) { sourceMap = findSourceMap(validFileName); diff --git a/benchmark/source_map/source-map.js b/benchmark/source_map/source-map.js index aed873b676ebbb..b5563302b47dda 100644 --- a/benchmark/source_map/source-map.js +++ b/benchmark/source_map/source-map.js @@ -16,15 +16,12 @@ const bench = common.createBenchmark( 'parse', 'parse-minified', 'parse-sectioned', - 'parse-large', 'findEntry', 'findEntry-minified', 'findEntry-sectioned', - 'findEntry-large', 'findOrigin', 'findOrigin-minified', 'findOrigin-sectioned', - 'findOrigin-large', ], n: [1e5], }, @@ -32,7 +29,7 @@ const bench = common.createBenchmark( ); function main({ operation, n }) { - const { SourceMap } = require('internal/source_map/source_map'); + const { SourceMap } = require('node:module'); const samplePayload = JSON.parse( fs.readFileSync( @@ -52,12 +49,6 @@ function main({ operation, n }) { 'utf8', ), ); - const largePayload = JSON.parse( - fs.readFileSync( - path.resolve(__dirname, '../../test/fixtures/test-runner/source-maps/line-lengths/index.js.map'), - 'utf8', - ), - ); let sourceMap; switch (operation) { @@ -85,14 +76,6 @@ function main({ operation, n }) { bench.end(n); break; - case 'parse-large': - bench.start(); - for (let i = 0; i < n; i++) { - sourceMap = new SourceMap(largePayload); - } - bench.end(n); - break; - case 'findEntry': sourceMap = new SourceMap(samplePayload); bench.start(); @@ -120,15 +103,6 @@ function main({ operation, n }) { bench.end(n); break; - case 'findEntry-large': - sourceMap = new SourceMap(largePayload); - bench.start(); - for (let i = 0; i < n; i++) { - sourceMap.findEntry(i, i); - } - bench.end(n); - break; - case 'findOrigin': sourceMap = new SourceMap(samplePayload); bench.start(); @@ -156,15 +130,6 @@ function main({ operation, n }) { bench.end(n); break; - case 'findOrigin-large': - sourceMap = new SourceMap(largePayload); - bench.start(); - for (let i = 0; i < n; i++) { - sourceMap.findOrigin(i, i); - } - bench.end(n); - break; - default: throw new Error(`Unknown operation: ${operation}`); } From 2d01a2b5b0bf153308eaebdfc01973d17d407f5f Mon Sep 17 00:00:00 2001 From: Miguel Marcondes Date: Mon, 9 Jun 2025 11:02:03 -0300 Subject: [PATCH 17/17] benchmark: remove prepare-stack-trace to avoid testing internals --- benchmark/source_map/prepare-stack-trace.js | 58 --------------------- 1 file changed, 58 deletions(-) delete mode 100644 benchmark/source_map/prepare-stack-trace.js diff --git a/benchmark/source_map/prepare-stack-trace.js b/benchmark/source_map/prepare-stack-trace.js deleted file mode 100644 index 18acd2d1c2fd06..00000000000000 --- a/benchmark/source_map/prepare-stack-trace.js +++ /dev/null @@ -1,58 +0,0 @@ -'use strict'; - -const common = require('../common.js'); -const assert = require('assert'); - -const options = { - flags: ['--expose-internals'], -}; - -const bench = common.createBenchmark( - main, - { - operation: ['non-node-error'], - n: [1e5], - }, - options, -); - -function ErrorGetCallSites() { - const originalStackFormatter = Error.prepareStackTrace; - Error.prepareStackTrace = (_err, stack) => { - if (stack && stack.length > 1) { - // Remove node:util - return stack.slice(1); - } - return stack; - }; - const err = new Error(); - // With the V8 Error API, the stack is not formatted until it is accessed - err.stack; // eslint-disable-line no-unused-expressions - Error.prepareStackTrace = originalStackFormatter; - return err.stack; -} - -function main({ operation, n }) { - const { prepareStackTraceWithSourceMaps } = require('internal/source_map/prepare_stack_trace'); - const { - ERR_ASSERTION, - } = require('internal/errors').codes; - - const nonNodeError = new ERR_ASSERTION('non Node error'); - const nonNodeErrorStackTrace = ErrorGetCallSites(); - - let preparedStackTrace; - switch (operation) { - case 'non-node-error': - bench.start(); - for (let i = 0; i < n; i++) { - preparedStackTrace = prepareStackTraceWithSourceMaps(nonNodeError, nonNodeErrorStackTrace); - } - bench.end(n); - break; - - default: - throw new Error(`Unknown operation: ${operation}`); - } - assert.ok(preparedStackTrace); -}