Skip to content

Commit 31252b9

Browse files
miguelmarcondesfRafaelGSS
authored andcommitted
benchmark: add source map and source map cache
PR-URL: #58125 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Edy Silva <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
1 parent 839b25e commit 31252b9

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const assert = require('assert');
5+
const fixtures = require('../../test/common/fixtures');
6+
7+
const bench = common.createBenchmark(
8+
main,
9+
{
10+
operation: [
11+
'findSourceMap-valid',
12+
'findSourceMap-generated-source',
13+
],
14+
n: [1e5],
15+
},
16+
);
17+
18+
function main({ operation, n }) {
19+
const Module = require('node:module');
20+
21+
Module.setSourceMapsSupport(true, {
22+
generatedCode: true,
23+
});
24+
const validFileName = fixtures.path('test-runner/source-maps/line-lengths/index.js');
25+
26+
const fileNameKey = '/source-map/disk.js';
27+
const generatedFileName = fixtures.path(fileNameKey);
28+
const generatedFileContent = fixtures.readSync(fileNameKey, 'utf8');
29+
const sourceMapUrl = generatedFileName.replace(/\.js$/, '.map');
30+
const sourceWithGeneratedSourceMap =
31+
`${generatedFileContent}\n//# sourceMappingURL=${sourceMapUrl}\n//# sourceURL=${generatedFileName}`;
32+
const generatedExpectedUrl = `file://${generatedFileName}`;
33+
34+
let sourceMap;
35+
switch (operation) {
36+
case 'findSourceMap-valid':
37+
require(validFileName);
38+
39+
bench.start();
40+
for (let i = 0; i < n; i++) {
41+
sourceMap = Module.findSourceMap(validFileName);
42+
}
43+
bench.end(n);
44+
break;
45+
46+
case 'findSourceMap-generated-source':
47+
eval(sourceWithGeneratedSourceMap);
48+
49+
bench.start();
50+
for (let i = 0; i < n; i++) {
51+
sourceMap = Module.findSourceMap(generatedExpectedUrl);
52+
}
53+
bench.end(n);
54+
break;
55+
56+
default:
57+
throw new Error(`Unknown operation: ${operation}`);
58+
}
59+
assert.ok(sourceMap);
60+
}

benchmark/source_map/source-map.js

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const assert = require('assert');
5+
const fixtures = require('../../test/common/fixtures');
6+
7+
const bench = common.createBenchmark(
8+
main,
9+
{
10+
operation: [
11+
'parse',
12+
'parse-sectioned',
13+
'findEntry',
14+
'findEntry-sectioned',
15+
'findOrigin',
16+
'findOrigin-sectioned',
17+
],
18+
n: [1e5],
19+
},
20+
);
21+
22+
function main({ operation, n }) {
23+
const { SourceMap } = require('node:module');
24+
25+
const samplePayload = JSON.parse(
26+
fixtures.readSync('source-map/no-source.js.map', 'utf8'),
27+
);
28+
const sectionedPayload = JSON.parse(
29+
fixtures.readSync('source-map/disk-index.map', 'utf8'),
30+
);
31+
32+
let sourceMap;
33+
let sourceMapMethod;
34+
switch (operation) {
35+
case 'parse':
36+
bench.start();
37+
for (let i = 0; i < n; i++) {
38+
sourceMap = new SourceMap(samplePayload);
39+
}
40+
bench.end(n);
41+
break;
42+
43+
case 'parse-sectioned':
44+
bench.start();
45+
for (let i = 0; i < n; i++) {
46+
sourceMap = new SourceMap(sectionedPayload);
47+
}
48+
bench.end(n);
49+
break;
50+
51+
case 'findEntry':
52+
sourceMap = new SourceMap(samplePayload);
53+
bench.start();
54+
for (let i = 0; i < n; i++) {
55+
sourceMapMethod = sourceMap.findEntry(i, i);
56+
}
57+
bench.end(n);
58+
assert.ok(sourceMapMethod);
59+
break;
60+
61+
case 'findEntry-sectioned':
62+
sourceMap = new SourceMap(sectionedPayload);
63+
bench.start();
64+
for (let i = 0; i < n; i++) {
65+
sourceMapMethod = sourceMap.findEntry(i, i);
66+
}
67+
bench.end(n);
68+
assert.ok(sourceMapMethod);
69+
break;
70+
71+
case 'findOrigin':
72+
sourceMap = new SourceMap(samplePayload);
73+
bench.start();
74+
for (let i = 0; i < n; i++) {
75+
sourceMapMethod = sourceMap.findOrigin(i, i);
76+
}
77+
bench.end(n);
78+
assert.ok(sourceMapMethod);
79+
break;
80+
81+
case 'findOrigin-sectioned':
82+
sourceMap = new SourceMap(sectionedPayload);
83+
bench.start();
84+
for (let i = 0; i < n; i++) {
85+
sourceMapMethod = sourceMap.findOrigin(i, i);
86+
}
87+
bench.end(n);
88+
assert.ok(sourceMapMethod);
89+
break;
90+
91+
default:
92+
throw new Error(`Unknown operation: ${operation}`);
93+
}
94+
assert.ok(sourceMap);
95+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const runBenchmark = require('../common/benchmark');
6+
7+
runBenchmark('source_map', { NODEJS_BENCHMARK_ZERO_ALLOWED: 1 });

0 commit comments

Comments
 (0)