diff --git a/benchmark/source_map/source-map-cache.js b/benchmark/source_map/source-map-cache.js new file mode 100644 index 00000000000000..210c3a437cc0ea --- /dev/null +++ b/benchmark/source_map/source-map-cache.js @@ -0,0 +1,73 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); + +const options = { + flags: ['--expose-internals'], +}; + +const bench = common.createBenchmark( + main, + { + operation: [ + 'findSourceMap-valid', + 'findSourceMap-generated-source', + ], + n: [1e5], + }, + options, +); + +function main({ operation, n }) { + const { + setSourceMapsSupport, + maybeCacheSourceMap, + } = require('internal/source_map/source_map_cache'); + const { findSourceMap } = require('node:module'); + + 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'); + const fakeModule = { filename: validFileName }; + + let sourceMap; + switch (operation) { + case 'findSourceMap-valid': + maybeCacheSourceMap(validFileName, validFileContent, fakeModule, false, undefined, validMapFile); + bench.start(); + + for (let i = 0; i < n; i++) { + sourceMap = findSourceMap(validFileName); + } + bench.end(n); + break; + + case 'findSourceMap-generated-source': + maybeCacheSourceMap( + validFileName, + validFileContent, + fakeModule, + true, + validFileName, + validMapFile, + ); + bench.start(); + + for (let i = 0; i < n; i++) { + sourceMap = findSourceMap(validFileName); + } + bench.end(n); + break; + + default: + throw new Error(`Unknown operation: ${operation}`); + } + assert.ok(sourceMap); +} diff --git a/benchmark/source_map/source-map.js b/benchmark/source_map/source-map.js new file mode 100644 index 00000000000000..b5563302b47dda --- /dev/null +++ b/benchmark/source_map/source-map.js @@ -0,0 +1,137 @@ +'use strict'; + +const common = require('../common.js'); +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); + +const options = { + flags: ['--expose-internals'], +}; + +const bench = common.createBenchmark( + main, + { + operation: [ + 'parse', + 'parse-minified', + 'parse-sectioned', + 'findEntry', + 'findEntry-minified', + 'findEntry-sectioned', + 'findOrigin', + 'findOrigin-minified', + 'findOrigin-sectioned', + ], + n: [1e5], + }, + options, +); + +function main({ operation, n }) { + const { SourceMap } = require('node:module'); + + 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', + ), + ); + + let sourceMap; + switch (operation) { + case 'parse': + bench.start(); + for (let i = 0; i < n; i++) { + sourceMap = new SourceMap(samplePayload); + } + bench.end(n); + break; + + case 'parse-minified': + bench.start(); + for (let i = 0; i < n; i++) { + sourceMap = new SourceMap(minifiedPayload); + } + bench.end(n); + break; + + case 'parse-sectioned': + bench.start(); + for (let i = 0; i < n; i++) { + sourceMap = new SourceMap(sectionedPayload); + } + bench.end(n); + break; + + case 'findEntry': + sourceMap = new SourceMap(samplePayload); + bench.start(); + for (let i = 0; i < n; i++) { + sourceMap.findEntry(i, i); + } + bench.end(n); + break; + + case 'findEntry-minified': + sourceMap = new SourceMap(minifiedPayload); + bench.start(); + for (let i = 0; i < n; i++) { + sourceMap.findEntry(i, i); + } + bench.end(n); + break; + + case 'findEntry-sectioned': + sourceMap = new SourceMap(sectionedPayload); + 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(); + for (let i = 0; i < n; i++) { + sourceMap.findOrigin(i, i); + } + bench.end(n); + break; + + case 'findOrigin-minified': + sourceMap = new SourceMap(minifiedPayload); + bench.start(); + for (let i = 0; i < n; i++) { + sourceMap.findOrigin(i, i); + } + bench.end(n); + break; + + case 'findOrigin-sectioned': + sourceMap = new SourceMap(sectionedPayload); + bench.start(); + for (let i = 0; i < n; i++) { + sourceMap.findOrigin(i, i); + } + bench.end(n); + break; + + default: + throw new Error(`Unknown operation: ${operation}`); + } + assert.ok(sourceMap); +} 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 });