Skip to content

Commit 205bed0

Browse files
nwoltmanrvagg
authored andcommitted
lib: copy arguments object instead of leaking it
Instead of leaking the arguments object by passing it as an argument to a function, copy it's contents to a new array, then pass the array. This allows V8 to optimize the function that contains this code, improving performance. PR-URL: #4361 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Brian White <mscdex@mscdex.net>
1 parent 9ebd559 commit 205bed0

File tree

5 files changed

+43
-14
lines changed

5 files changed

+43
-14
lines changed

lib/_http_client.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,10 +608,18 @@ ClientRequest.prototype.setTimeout = function(msecs, callback) {
608608
};
609609

610610
ClientRequest.prototype.setNoDelay = function() {
611-
this._deferToConnect('setNoDelay', arguments);
611+
const argsLen = arguments.length;
612+
const args = new Array(argsLen);
613+
for (var i = 0; i < argsLen; i++)
614+
args[i] = arguments[i];
615+
this._deferToConnect('setNoDelay', args);
612616
};
613617
ClientRequest.prototype.setSocketKeepAlive = function() {
614-
this._deferToConnect('setKeepAlive', arguments);
618+
const argsLen = arguments.length;
619+
const args = new Array(argsLen);
620+
for (var i = 0; i < argsLen; i++)
621+
args[i] = arguments[i];
622+
this._deferToConnect('setKeepAlive', args);
615623
};
616624

617625
ClientRequest.prototype.clearTimeout = function(cb) {

lib/_tls_wrap.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,11 @@ function normalizeConnectArgs(listArgs) {
972972
}
973973

974974
exports.connect = function(/* [port, host], options, cb */) {
975-
var args = normalizeConnectArgs(arguments);
975+
const argsLen = arguments.length;
976+
var args = new Array(argsLen);
977+
for (var i = 0; i < argsLen; i++)
978+
args[i] = arguments[i];
979+
args = normalizeConnectArgs(args);
976980
var options = args[0];
977981
var cb = args[1];
978982

lib/assert.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,16 @@ function expectedException(actual, expected) {
290290
return expected.call({}, actual) === true;
291291
}
292292

293+
function _tryBlock(block) {
294+
var error;
295+
try {
296+
block();
297+
} catch (e) {
298+
error = e;
299+
}
300+
return error;
301+
}
302+
293303
function _throws(shouldThrow, block, expected, message) {
294304
var actual;
295305

@@ -302,11 +312,7 @@ function _throws(shouldThrow, block, expected, message) {
302312
expected = null;
303313
}
304314

305-
try {
306-
block();
307-
} catch (e) {
308-
actual = e;
309-
}
315+
actual = _tryBlock(block);
310316

311317
message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
312318
(message ? ' ' + message : '.');
@@ -329,12 +335,12 @@ function _throws(shouldThrow, block, expected, message) {
329335
// assert.throws(block, Error_opt, message_opt);
330336

331337
assert.throws = function(block, /*optional*/error, /*optional*/message) {
332-
_throws.apply(this, [true].concat(pSlice.call(arguments)));
338+
_throws(true, block, error, message);
333339
};
334340

335341
// EXTENSION! This is annoying to write outside this module.
336-
assert.doesNotThrow = function(block, /*optional*/message) {
337-
_throws.apply(this, [false].concat(pSlice.call(arguments)));
342+
assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
343+
_throws(false, block, error, message);
338344
};
339345

340346
assert.ifError = function(err) { if (err) throw err; };

lib/console.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ Console.prototype.trace = function trace() {
8484

8585
Console.prototype.assert = function(expression) {
8686
if (!expression) {
87-
var arr = Array.prototype.slice.call(arguments, 1);
87+
const argsLen = arguments.length || 1;
88+
const arr = new Array(argsLen - 1);
89+
for (var i = 1; i < argsLen; i++)
90+
arr[i - 1] = arguments[i];
8891
require('assert').ok(false, util.format.apply(null, arr));
8992
}
9093
};

lib/net.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ exports.createServer = function(options, connectionListener) {
5959
// connect(path, [cb]);
6060
//
6161
exports.connect = exports.createConnection = function() {
62-
var args = normalizeConnectArgs(arguments);
62+
const argsLen = arguments.length;
63+
var args = new Array(argsLen);
64+
for (var i = 0; i < argsLen; i++)
65+
args[i] = arguments[i];
66+
args = normalizeConnectArgs(args);
6367
debug('createConnection', args);
6468
var s = new Socket(args[0]);
6569
return Socket.prototype.connect.apply(s, args);
@@ -858,7 +862,11 @@ Socket.prototype.connect = function(options, cb) {
858862
// Old API:
859863
// connect(port, [host], [cb])
860864
// connect(path, [cb]);
861-
var args = normalizeConnectArgs(arguments);
865+
const argsLen = arguments.length;
866+
var args = new Array(argsLen);
867+
for (var i = 0; i < argsLen; i++)
868+
args[i] = arguments[i];
869+
args = normalizeConnectArgs(args);
862870
return Socket.prototype.connect.apply(this, args);
863871
}
864872

0 commit comments

Comments
 (0)