Skip to content

Commit d5f2f14

Browse files
legendecasnodejs-github-bot
authored andcommitted
benchmark: add microbench on isInsideNodeModules
PR-URL: #60991 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Daniel Lemire <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 83ba6b1 commit d5f2f14

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const assert = require('assert');
4+
5+
const bench = common.createBenchmark(main, {
6+
n: [1e6],
7+
stackLimit: [100],
8+
stackCount: [99, 101],
9+
method: ['isInsideNodeModules', 'noop'],
10+
}, {
11+
flags: ['--expose-internals', '--disable-warning=internal/test/binding'],
12+
});
13+
14+
function main({ n, stackLimit, stackCount, method }) {
15+
const { internalBinding } = require('internal/test/binding');
16+
const { isInsideNodeModules } = internalBinding('util');
17+
18+
const testFunction = method === 'noop' ?
19+
() => {
20+
bench.start();
21+
for (let i = 0; i < n; i++) {
22+
noop();
23+
}
24+
bench.end(n);
25+
} :
26+
() => {
27+
Error.stackTraceLimit = Infinity;
28+
const existingStackFrameCount = new Error().stack.split('\n').length - 1;
29+
assert.strictEqual(existingStackFrameCount, stackCount);
30+
31+
bench.start();
32+
for (let i = 0; i < n; i++) {
33+
isInsideNodeModules(stackLimit, true);
34+
}
35+
bench.end(n);
36+
};
37+
38+
// Excluding the message line.
39+
const existingStackFrameCount = new Error().stack.split('\n').length - 1;
40+
// Excluding the test function itself.
41+
nestCallStack(stackCount - existingStackFrameCount - 1, testFunction);
42+
}
43+
44+
function nestCallStack(depth, callback) {
45+
// nestCallStack(1) already adds a stack frame, so we stop at 1.
46+
if (depth === 1) {
47+
return callback();
48+
}
49+
return nestCallStack(depth - 1, callback);
50+
}
51+
52+
function noop() {}

0 commit comments

Comments
 (0)