Skip to content

Commit ef1e77d

Browse files
Trottjasnell
authored andcommitted
vm: improve performance of vm.runIn*()
Optimize for common cases in vm.runInContext() and vm.runInThisContext(). PR-URL: #10816 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent 030dd14 commit ef1e77d

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

lib/vm.js

+11-15
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,17 @@ const realRunInThisContext = Script.prototype.runInThisContext;
1717
const realRunInContext = Script.prototype.runInContext;
1818

1919
Script.prototype.runInThisContext = function(options) {
20-
if (options && options.breakOnSigint) {
21-
const realRunInThisContextScript = () => {
22-
return realRunInThisContext.call(this, options);
23-
};
24-
return sigintHandlersWrap(realRunInThisContextScript);
20+
if (options && options.breakOnSigint && process._events.SIGINT) {
21+
return sigintHandlersWrap(realRunInThisContext, this, [options]);
2522
} else {
2623
return realRunInThisContext.call(this, options);
2724
}
2825
};
2926

3027
Script.prototype.runInContext = function(contextifiedSandbox, options) {
31-
if (options && options.breakOnSigint) {
32-
const realRunInContextScript = () => {
33-
return realRunInContext.call(this, contextifiedSandbox, options);
34-
};
35-
return sigintHandlersWrap(realRunInContextScript);
28+
if (options && options.breakOnSigint && process._events.SIGINT) {
29+
return sigintHandlersWrap(realRunInContext, this,
30+
[contextifiedSandbox, options]);
3631
} else {
3732
return realRunInContext.call(this, contextifiedSandbox, options);
3833
}
@@ -83,19 +78,20 @@ exports.isContext = binding.isContext;
8378

8479
// Remove all SIGINT listeners and re-attach them after the wrapped function
8580
// has executed, so that caught SIGINT are handled by the listeners again.
86-
function sigintHandlersWrap(fn) {
81+
function sigintHandlersWrap(fn, thisArg, argsArray) {
8782
// Using the internal list here to make sure `.once()` wrappers are used,
8883
// not the original ones.
8984
let sigintListeners = process._events.SIGINT;
90-
if (!Array.isArray(sigintListeners))
91-
sigintListeners = sigintListeners ? [sigintListeners] : [];
92-
else
85+
86+
if (Array.isArray(sigintListeners))
9387
sigintListeners = sigintListeners.slice();
88+
else
89+
sigintListeners = [sigintListeners];
9490

9591
process.removeAllListeners('SIGINT');
9692

9793
try {
98-
return fn();
94+
return fn.apply(thisArg, argsArray);
9995
} finally {
10096
// Add using the public methods so that the `newListener` handler of
10197
// process can re-attach the listeners.

0 commit comments

Comments
 (0)