Skip to content

Commit 3c8e59c

Browse files
nwoltmanMyles Borins
authored and
Myles Borins
committed
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 <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent eb0ed46 commit 3c8e59c

File tree

5 files changed

+43
-14
lines changed

5 files changed

+43
-14
lines changed

lib/_http_client.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,18 @@ ClientRequest.prototype.setTimeout = function(msecs, callback) {
569569
};
570570

571571
ClientRequest.prototype.setNoDelay = function() {
572-
this._deferToConnect('setNoDelay', arguments);
572+
const argsLen = arguments.length;
573+
const args = new Array(argsLen);
574+
for (var i = 0; i < argsLen; i++)
575+
args[i] = arguments[i];
576+
this._deferToConnect('setNoDelay', args);
573577
};
574578
ClientRequest.prototype.setSocketKeepAlive = function() {
575-
this._deferToConnect('setKeepAlive', arguments);
579+
const argsLen = arguments.length;
580+
const args = new Array(argsLen);
581+
for (var i = 0; i < argsLen; i++)
582+
args[i] = arguments[i];
583+
this._deferToConnect('setKeepAlive', args);
576584
};
577585

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

lib/_tls_wrap.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,11 @@ function normalizeConnectArgs(listArgs) {
944944
}
945945

946946
exports.connect = function(/* [port, host], options, cb */) {
947-
var args = normalizeConnectArgs(arguments);
947+
const argsLen = arguments.length;
948+
var args = new Array(argsLen);
949+
for (var i = 0; i < argsLen; i++)
950+
args[i] = arguments[i];
951+
args = normalizeConnectArgs(args);
948952
var options = args[0];
949953
var cb = args[1];
950954

lib/assert.js

+14-8
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

+4-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ Console.prototype.trace = function trace() {
8383

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

lib/net.js

+10-2
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)