Skip to content

Commit d0582ef

Browse files
nwoltmanjasnell
authored andcommittedMar 11, 2016
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 0ea3899 commit d0582ef

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
@@ -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

+5-1
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,11 @@ function normalizeConnectArgs(listArgs) {
960960
}
961961

962962
exports.connect = function(/* [port, host], options, cb */) {
963-
var args = normalizeConnectArgs(arguments);
963+
const argsLen = arguments.length;
964+
var args = new Array(argsLen);
965+
for (var i = 0; i < argsLen; i++)
966+
args[i] = arguments[i];
967+
args = normalizeConnectArgs(args);
964968
var options = args[0];
965969
var cb = args[1];
966970

‎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
@@ -85,7 +85,10 @@ Console.prototype.trace = function trace() {
8585

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

‎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)