node
node copied to clipboard
vm: perform lazy-loading at top level
As it currently stands, the lazy loading for lib/internal/vm/module.js's require of internal/util/inspect is performed in a function. This means that a re-call of the function will be a re-call of the require of the module.
This PR moves the lazy-loading to only occur once.
@RedYetiDev How big a difference is it considering the require cache?
top_level.js
const vm = require('vm');
for (let i = 0; i < 1_000_000; i++) {
vm;
1+1;
}
vs
in_func.js
for (let i = 0; i < 1_000_000; i++) {
const vm = require('vm');
1+1;
}
I got:
Benchmark 1: node in_func.js
Time (mean ± σ): 218.0 ms ± 10.2 ms [User: 219.2 ms, System: 12.4 ms]
Range (min … max): 201.5 ms … 228.3 ms 14 runs
Benchmark 2: node top_level.js
Time (mean ± σ): 17.9 ms ± 0.5 ms [User: 12.1 ms, System: 6.9 ms]
Range (min … max): 17.0 ms … 21.1 ms 162 runs
Summary
node top_level.js ran
12.20 ± 0.66 times faster than node in_func.js
Note this benchmark is in no way accurate, it's just a basic demo, and it was really just put together in 10 seconds.