From cf3a4030e82e01960bb3dc2849274b025f87d69f Mon Sep 17 00:00:00 2001 From: Minwoo Jung Date: Fri, 21 Aug 2015 14:30:08 -0700 Subject: [PATCH] lib: use arrow functions instead of bind use `arrow functions` instead of `bind(this)` in order to improve performance through optimizations. PR-URL: https://github.com/nodejs/node/pull/3622 Reviewed-By: Jeremiah Senkpiel Reviewed-By: Trevor Norris Reviewed-By: James M Snell Reviewed-By: Myles Borins --- lib/_debugger.js | 16 +++++++++------- lib/_tls_legacy.js | 11 ++++++----- lib/_tls_wrap.js | 14 +++++++------- lib/cluster.js | 12 +++++++++--- lib/fs.js | 2 +- lib/net.js | 6 +++--- 6 files changed, 35 insertions(+), 26 deletions(-) diff --git a/lib/_debugger.js b/lib/_debugger.js index 0815b037dc..bd655ff45c 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -159,7 +159,7 @@ function Client() { protocol.execute(d); }); - protocol.onResponse = this._onResponse.bind(this); + protocol.onResponse = (res) => this._onResponse(res); } inherits(Client, net.Stream); exports.Client = Client; @@ -733,7 +733,7 @@ function Interface(stdin, stdout, args) { prompt: 'debug> ', input: this.stdin, output: this.stdout, - eval: this.controlEval.bind(this), + eval: (code, ctx, file, cb) => this.controlEval(code, ctx, file, cb), useGlobal: false, ignoreUndefined: true }; @@ -764,7 +764,7 @@ function Interface(stdin, stdout, args) { }); // Handle all possible exits - process.on('exit', this.killChild.bind(this)); + process.on('exit', () => this.killChild()); process.once('SIGTERM', process.exit.bind(process, 0)); process.once('SIGHUP', process.exit.bind(process, 0)); @@ -1566,7 +1566,8 @@ Interface.prototype.repl = function() { this.repl.on('exit', exitDebugRepl); // Set new - this.repl.eval = this.debugEval.bind(this); + this.repl.eval = (code, ctx, file, cb) => + this.debugEval(code, ctx, file, cb); this.repl.context = {}; // Swap history @@ -1581,7 +1582,8 @@ Interface.prototype.repl = function() { // Exit debug repl Interface.prototype.exitRepl = function() { // Restore eval - this.repl.eval = this.controlEval.bind(this); + this.repl.eval = (code, ctx, file, cb) => + this.controlEval(code, ctx, file, cb); // Swap history this.history.debug = this.repl.rli.history; @@ -1668,8 +1670,8 @@ Interface.prototype.trySpawn = function(cb) { // pipe stream into debugger this.child = spawn(process.execPath, childArgs); - this.child.stdout.on('data', this.childPrint.bind(this)); - this.child.stderr.on('data', this.childPrint.bind(this)); + this.child.stdout.on('data', (text) => this.childPrint(text)); + this.child.stderr.on('data', (text) => this.childPrint(text)); } this.pause(); diff --git a/lib/_tls_legacy.js b/lib/_tls_legacy.js index 54ffb0a903..bf79d7c867 100644 --- a/lib/_tls_legacy.js +++ b/lib/_tls_legacy.js @@ -706,14 +706,15 @@ function SecurePair(context, isServer, requestCert, rejectUnauthorized, this._rejectUnauthorized); if (this._isServer) { - this.ssl.onhandshakestart = onhandshakestart.bind(this); - this.ssl.onhandshakedone = onhandshakedone.bind(this); - this.ssl.onclienthello = onclienthello.bind(this); - this.ssl.onnewsession = onnewsession.bind(this); + this.ssl.onhandshakestart = () => onhandshakestart.call(this); + this.ssl.onhandshakedone = () => onhandshakedone.call(this); + this.ssl.onclienthello = (hello) => onclienthello.call(this, hello); + this.ssl.onnewsession = + (key, session) => onnewsession.call(this, key, session); this.ssl.lastHandshakeTime = 0; this.ssl.handshakes = 0; } else { - this.ssl.onocspresponse = onocspresponse.bind(this); + this.ssl.onocspresponse = (resp) => onocspresponse.call(this, resp); } if (process.features.tls_sni) { diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index 35d5ba3b38..ca5933c8ab 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -406,11 +406,11 @@ TLSSocket.prototype._init = function(socket, wrap) { ssl.setVerifyMode(requestCert, rejectUnauthorized); if (options.isServer) { - ssl.onhandshakestart = onhandshakestart.bind(this); - ssl.onhandshakedone = onhandshakedone.bind(this); - ssl.onclienthello = onclienthello.bind(this); - ssl.oncertcb = oncertcb.bind(this); - ssl.onnewsession = onnewsession.bind(this); + ssl.onhandshakestart = () => onhandshakestart.call(this); + ssl.onhandshakedone = () => onhandshakedone.call(this); + ssl.onclienthello = (hello) => onclienthello.call(this, hello); + ssl.oncertcb = (info) => oncertcb.call(this, info); + ssl.onnewsession = (key, session) => onnewsession.call(this, key, session); ssl.lastHandshakeTime = 0; ssl.handshakes = 0; @@ -424,8 +424,8 @@ TLSSocket.prototype._init = function(socket, wrap) { } } else { ssl.onhandshakestart = function() {}; - ssl.onhandshakedone = this._finishInit.bind(this); - ssl.onocspresponse = onocspresponse.bind(this); + ssl.onhandshakedone = () => this._finishInit(); + ssl.onocspresponse = (resp) => onocspresponse.call(this, resp); if (options.session) ssl.setSession(options.session); diff --git a/lib/cluster.js b/lib/cluster.js index 82e0e4c628..c8bf658d5b 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -33,8 +33,12 @@ function Worker(options) { if (options.process) { this.process = options.process; - this.process.on('error', this.emit.bind(this, 'error')); - this.process.on('message', this.emit.bind(this, 'message')); + this.process.on('error', (code, signal) => + this.emit('error', code, signal) + ); + this.process.on('message', (message, handle) => + this.emit('message', message, handle) + ); } } util.inherits(Worker, EventEmitter); @@ -337,7 +341,9 @@ function masterInit() { process: workerProcess }); - worker.on('message', this.emit.bind(this, 'message')); + worker.on('message', (message, handle) => + this.emit('message', message, handle) + ); worker.process.once('exit', function(exitCode, signalCode) { /* diff --git a/lib/fs.js b/lib/fs.js index 81f8a3c3dd..7bfc21c8d0 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1763,7 +1763,7 @@ ReadStream.prototype.close = function(cb) { this.once('open', close); return; } - return process.nextTick(this.emit.bind(this, 'close')); + return process.nextTick(() => this.emit('close')); } this.closed = true; close(); diff --git a/lib/net.js b/lib/net.js index 0627116350..7a612096ab 100644 --- a/lib/net.js +++ b/lib/net.js @@ -322,7 +322,7 @@ Socket.prototype._onTimeout = function() { Socket.prototype.setNoDelay = function(enable) { if (!this._handle) { this.once('connect', - enable ? this.setNoDelay : this.setNoDelay.bind(this, enable)); + enable ? this.setNoDelay : () => this.setNoDelay(enable)); return this; } @@ -336,7 +336,7 @@ Socket.prototype.setNoDelay = function(enable) { Socket.prototype.setKeepAlive = function(setting, msecs) { if (!this._handle) { - this.once('connect', this.setKeepAlive.bind(this, setting, msecs)); + this.once('connect', () => this.setKeepAlive(setting, msecs)); return this; } @@ -384,7 +384,7 @@ Socket.prototype._read = function(n) { if (this._connecting || !this._handle) { debug('_read wait for connection'); - this.once('connect', this._read.bind(this, n)); + this.once('connect', () => this._read(n)); } else if (!this._handle.reading) { // not already reading, start the flow debug('Socket._read readStart');