From c94f346b932e5bda66c31de27b3302e495000b47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Mon, 5 Jun 2017 14:05:36 +0200 Subject: [PATCH] net: use rest parameters instead of arguments In v8 6.0, rest parameters are significantly faster than other ways to create an array of the arguments, even for small numbers. PR-URL: https://github.com/nodejs/node/pull/13472 Refs: https://github.com/nodejs/node/issues/13430 Reviewed-By: Matteo Collina Reviewed-By: Refael Ackermann Reviewed-By: James M Snell Reviewed-By: Joyee Cheung --- lib/net.js | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/lib/net.js b/lib/net.js index f07b8cd2c72f09..220847ac11cbda 100644 --- a/lib/net.js +++ b/lib/net.js @@ -91,11 +91,7 @@ function createServer(options, connectionListener) { // connect(port, [host], [cb]) // connect(path, [cb]); // -function connect() { - var args = new Array(arguments.length); - for (var i = 0; i < arguments.length; i++) - args[i] = arguments[i]; - // TODO(joyeecheung): use destructuring when V8 is fast enough +function connect(...args) { var normalized = normalizeArgs(args); var options = normalized[0]; debug('createConnection', normalized); @@ -948,19 +944,15 @@ function internalConnect( } -Socket.prototype.connect = function() { +Socket.prototype.connect = function(...args) { let normalized; // If passed an array, it's treated as an array of arguments that have // already been normalized (so we don't normalize more than once). This has // been solved before in https://github.com/nodejs/node/pull/12342, but was // reverted as it had unintended side effects. - if (Array.isArray(arguments[0]) && arguments[0][normalizedArgsSymbol]) { - normalized = arguments[0]; + if (Array.isArray(args[0]) && args[0][normalizedArgsSymbol]) { + normalized = args[0]; } else { - var args = new Array(arguments.length); - for (var i = 0; i < arguments.length; i++) - args[i] = arguments[i]; - // TODO(joyeecheung): use destructuring when V8 is fast enough normalized = normalizeArgs(args); } var options = normalized[0]; @@ -1420,11 +1412,7 @@ function listenInCluster(server, address, port, addressType, } -Server.prototype.listen = function() { - var args = new Array(arguments.length); - for (var i = 0; i < arguments.length; i++) - args[i] = arguments[i]; - // TODO(joyeecheung): use destructuring when V8 is fast enough +Server.prototype.listen = function(...args) { var normalized = normalizeArgs(args); var options = normalized[0]; var cb = normalized[1];