Repository: nodejs/node
Pull request: Add SourceMap Benchmark

## Add SourceMap Benchmark

This PR introduces a new benchmark suite for the `lib/internal/source_map` module to help evaluate and explore the performance characteristics of the `SourceMap`.

I'm still learning how everything fits together, but this is a first step toward understanding where small improvements could improve performance.

cc @BridgeAR  

Changed files:
filename: benchmark/source_map/source-map-cache.js
patch: @@ -0,0 +1,60 @@
+'use strict';
+
+const common = require('../common.js');
+const assert = require('assert');
+const fixtures = require('../../test/common/fixtures');
+
+const bench = common.createBenchmark(
+  main,
+  {
+    operation: [
+      'findSourceMap-valid',
+      'findSourceMap-generated-source',
+    ],
+    n: [1e5],
+  },
+);
+
+function main({ operation, n }) {
+  const Module = require('node:module');
+
+  Module.setSourceMapsSupport(true, {
+    generatedCode: true,
+  });
+  const validFileName = fixtures.path('test-runner/source-maps/line-lengths/index.js');
+
+  const fileNameKey = '/source-map/disk.js';
+  const generatedFileName = fixtures.path(fileNameKey);
+  const generatedFileContent = fixtures.readSync(fileNameKey, 'utf8');
+  const sourceMapUrl = generatedFileName.replace(/\.js$/, '.map');
+  const sourceWithGeneratedSourceMap =
+    `${generatedFileContent}\n//# sourceMappingURL=${sourceMapUrl}\n//# sourceURL=${generatedFileName}`;
+  const generatedExpectedUrl = `file://${generatedFileName}`;
+
+  let sourceMap;
+  switch (operation) {
+    case 'findSourceMap-valid':
+      require(validFileName);
+
+      bench.start();
+      for (let i = 0; i < n; i++) {
+        sourceMap = Module.findSourceMap(validFileName);
+      }
+      bench.end(n);
+      break;
+
+    case 'findSourceMap-generated-source':
+      eval(sourceWithGeneratedSourceMap);
+
+      bench.start();
+      for (let i = 0; i < n; i++) {
+        sourceMap = Module.findSourceMap(generatedExpectedUrl);
+      }
+      bench.end(n);
+      break;
+
+    default:
+      throw new Error(`Unknown operation: ${operation}`);
+  }
+  assert.ok(sourceMap);
+}

filename: benchmark/source_map/source-map.js
patch: @@ -0,0 +1,95 @@
+'use strict';
+
+const common = require('../common.js');
+const assert = require('assert');
+const fixtures = require('../../test/common/fixtures');
+
+const bench = common.createBenchmark(
+  main,
+  {
+    operation: [
+      'parse',
+      'parse-sectioned',
+      'findEntry',
+      'findEntry-sectioned',
+      'findOrigin',
+      'findOrigin-sectioned',
+    ],
+    n: [1e5],
+  },
+);
+
+function main({ operation, n }) {
+  const { SourceMap } = require('node:module');
+
+  const samplePayload = JSON.parse(
+    fixtures.readSync('source-map/no-source.js.map', 'utf8'),
+  );
+  const sectionedPayload = JSON.parse(
+    fixtures.readSync('source-map/disk-index.map', 'utf8'),
+  );
+
+  let sourceMap;
+  let sourceMapMethod;
+  switch (operation) {
+    case 'parse':
+      bench.start();
+      for (let i = 0; i < n; i++) {
+        sourceMap = new SourceMap(samplePayload);
+      }
+      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++) {
+        sourceMapMethod = sourceMap.findEntry(i, i);
+      }
+      bench.end(n);
+      assert.ok(sourceMapMethod);
+      break;
+
+    case 'findEntry-sectioned':
+      sourceMap = new SourceMap(sectionedPayload);
+      bench.start();
+      for (let i = 0; i < n; i++) {
+        sourceMapMethod = sourceMap.findEntry(i, i);
+      }
+      bench.end(n);
+      assert.ok(sourceMapMethod);
+      break;
+
+    case 'findOrigin':
+      sourceMap = new SourceMap(samplePayload);
+      bench.start();
+      for (let i = 0; i < n; i++) {
+        sourceMapMethod = sourceMap.findOrigin(i, i);
+      }
+      bench.end(n);
+      assert.ok(sourceMapMethod);
+      break;
+
+    case 'findOrigin-sectioned':
+      sourceMap = new SourceMap(sectionedPayload);
+      bench.start();
+      for (let i = 0; i < n; i++) {
+        sourceMapMethod = sourceMap.findOrigin(i, i);
+      }
+      bench.end(n);
+      assert.ok(sourceMapMethod);
+      break;
+
+    default:
+      throw new Error(`Unknown operation: ${operation}`);
+  }
+  assert.ok(sourceMap);
+}

filename: test/benchmark/test-benchmark-source-map.js
patch: @@ -0,0 +1,7 @@
+'use strict';
+
+require('../common');
+
+const runBenchmark = require('../common/benchmark');
+
+runBenchmark('source_map', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });