forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_isinsidenodemodules.js
More file actions
51 lines (44 loc) · 1.37 KB
/
util_isinsidenodemodules.js
File metadata and controls
51 lines (44 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'use strict';
const common = require('../common.js');
const assert = require('assert');
const bench = common.createBenchmark(main, {
n: [1e6],
stackCount: [99, 101],
method: ['isInsideNodeModules', 'noop'],
}, {
flags: ['--expose-internals', '--disable-warning=internal/test/binding'],
});
function main({ n, stackCount, method }) {
const { internalBinding } = require('internal/test/binding');
const { isInsideNodeModules } = internalBinding('util');
const testFunction = method === 'noop' ?
() => {
bench.start();
for (let i = 0; i < n; i++) {
noop();
}
bench.end(n);
} :
() => {
Error.stackTraceLimit = Infinity;
const existingStackFrameCount = new Error().stack.split('\n').length - 1;
assert.strictEqual(existingStackFrameCount, stackCount);
bench.start();
for (let i = 0; i < n; i++) {
isInsideNodeModules();
}
bench.end(n);
};
// Excluding the message line.
const existingStackFrameCount = new Error().stack.split('\n').length - 1;
// Excluding the test function itself.
nestCallStack(stackCount - existingStackFrameCount - 1, testFunction);
}
function nestCallStack(depth, callback) {
// nestCallStack(1) already adds a stack frame, so we stop at 1.
if (depth === 1) {
return callback();
}
return nestCallStack(depth - 1, callback);
}
function noop() {}