Skip to content

Commit 59255d7

Browse files
JungMinuMyles Borins
authored and
Myles Borins
committed
lib: use arrow functions instead of bind
use `arrow functions` instead of `bind(this)` in order to improve performance through optimizations. PR-URL: #3622 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Myles Borins <[email protected]>
1 parent c18ef54 commit 59255d7

File tree

6 files changed

+35
-26
lines changed

6 files changed

+35
-26
lines changed

lib/_debugger.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ function Client() {
159159
protocol.execute(d);
160160
});
161161

162-
protocol.onResponse = this._onResponse.bind(this);
162+
protocol.onResponse = (res) => this._onResponse(res);
163163
}
164164
inherits(Client, net.Stream);
165165
exports.Client = Client;
@@ -733,7 +733,7 @@ function Interface(stdin, stdout, args) {
733733
prompt: 'debug> ',
734734
input: this.stdin,
735735
output: this.stdout,
736-
eval: this.controlEval.bind(this),
736+
eval: (code, ctx, file, cb) => this.controlEval(code, ctx, file, cb),
737737
useGlobal: false,
738738
ignoreUndefined: true
739739
};
@@ -764,7 +764,7 @@ function Interface(stdin, stdout, args) {
764764
});
765765

766766
// Handle all possible exits
767-
process.on('exit', this.killChild.bind(this));
767+
process.on('exit', () => this.killChild());
768768
process.once('SIGTERM', process.exit.bind(process, 0));
769769
process.once('SIGHUP', process.exit.bind(process, 0));
770770

@@ -1566,7 +1566,8 @@ Interface.prototype.repl = function() {
15661566
this.repl.on('exit', exitDebugRepl);
15671567

15681568
// Set new
1569-
this.repl.eval = this.debugEval.bind(this);
1569+
this.repl.eval = (code, ctx, file, cb) =>
1570+
this.debugEval(code, ctx, file, cb);
15701571
this.repl.context = {};
15711572

15721573
// Swap history
@@ -1581,7 +1582,8 @@ Interface.prototype.repl = function() {
15811582
// Exit debug repl
15821583
Interface.prototype.exitRepl = function() {
15831584
// Restore eval
1584-
this.repl.eval = this.controlEval.bind(this);
1585+
this.repl.eval = (code, ctx, file, cb) =>
1586+
this.controlEval(code, ctx, file, cb);
15851587

15861588
// Swap history
15871589
this.history.debug = this.repl.rli.history;
@@ -1668,8 +1670,8 @@ Interface.prototype.trySpawn = function(cb) {
16681670
// pipe stream into debugger
16691671
this.child = spawn(process.execPath, childArgs);
16701672

1671-
this.child.stdout.on('data', this.childPrint.bind(this));
1672-
this.child.stderr.on('data', this.childPrint.bind(this));
1673+
this.child.stdout.on('data', (text) => this.childPrint(text));
1674+
this.child.stderr.on('data', (text) => this.childPrint(text));
16731675
}
16741676

16751677
this.pause();

lib/_tls_legacy.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -706,14 +706,15 @@ function SecurePair(context, isServer, requestCert, rejectUnauthorized,
706706
this._rejectUnauthorized);
707707

708708
if (this._isServer) {
709-
this.ssl.onhandshakestart = onhandshakestart.bind(this);
710-
this.ssl.onhandshakedone = onhandshakedone.bind(this);
711-
this.ssl.onclienthello = onclienthello.bind(this);
712-
this.ssl.onnewsession = onnewsession.bind(this);
709+
this.ssl.onhandshakestart = () => onhandshakestart.call(this);
710+
this.ssl.onhandshakedone = () => onhandshakedone.call(this);
711+
this.ssl.onclienthello = (hello) => onclienthello.call(this, hello);
712+
this.ssl.onnewsession =
713+
(key, session) => onnewsession.call(this, key, session);
713714
this.ssl.lastHandshakeTime = 0;
714715
this.ssl.handshakes = 0;
715716
} else {
716-
this.ssl.onocspresponse = onocspresponse.bind(this);
717+
this.ssl.onocspresponse = (resp) => onocspresponse.call(this, resp);
717718
}
718719

719720
if (process.features.tls_sni) {

lib/_tls_wrap.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,11 @@ TLSSocket.prototype._init = function(socket, wrap) {
406406
ssl.setVerifyMode(requestCert, rejectUnauthorized);
407407

408408
if (options.isServer) {
409-
ssl.onhandshakestart = onhandshakestart.bind(this);
410-
ssl.onhandshakedone = onhandshakedone.bind(this);
411-
ssl.onclienthello = onclienthello.bind(this);
412-
ssl.oncertcb = oncertcb.bind(this);
413-
ssl.onnewsession = onnewsession.bind(this);
409+
ssl.onhandshakestart = () => onhandshakestart.call(this);
410+
ssl.onhandshakedone = () => onhandshakedone.call(this);
411+
ssl.onclienthello = (hello) => onclienthello.call(this, hello);
412+
ssl.oncertcb = (info) => oncertcb.call(this, info);
413+
ssl.onnewsession = (key, session) => onnewsession.call(this, key, session);
414414
ssl.lastHandshakeTime = 0;
415415
ssl.handshakes = 0;
416416

@@ -424,8 +424,8 @@ TLSSocket.prototype._init = function(socket, wrap) {
424424
}
425425
} else {
426426
ssl.onhandshakestart = function() {};
427-
ssl.onhandshakedone = this._finishInit.bind(this);
428-
ssl.onocspresponse = onocspresponse.bind(this);
427+
ssl.onhandshakedone = () => this._finishInit();
428+
ssl.onocspresponse = (resp) => onocspresponse.call(this, resp);
429429

430430
if (options.session)
431431
ssl.setSession(options.session);

lib/cluster.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ function Worker(options) {
3333

3434
if (options.process) {
3535
this.process = options.process;
36-
this.process.on('error', this.emit.bind(this, 'error'));
37-
this.process.on('message', this.emit.bind(this, 'message'));
36+
this.process.on('error', (code, signal) =>
37+
this.emit('error', code, signal)
38+
);
39+
this.process.on('message', (message, handle) =>
40+
this.emit('message', message, handle)
41+
);
3842
}
3943
}
4044
util.inherits(Worker, EventEmitter);
@@ -337,7 +341,9 @@ function masterInit() {
337341
process: workerProcess
338342
});
339343

340-
worker.on('message', this.emit.bind(this, 'message'));
344+
worker.on('message', (message, handle) =>
345+
this.emit('message', message, handle)
346+
);
341347

342348
worker.process.once('exit', function(exitCode, signalCode) {
343349
/*

lib/fs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,7 @@ ReadStream.prototype.close = function(cb) {
17631763
this.once('open', close);
17641764
return;
17651765
}
1766-
return process.nextTick(this.emit.bind(this, 'close'));
1766+
return process.nextTick(() => this.emit('close'));
17671767
}
17681768
this.closed = true;
17691769
close();

lib/net.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ Socket.prototype._onTimeout = function() {
322322
Socket.prototype.setNoDelay = function(enable) {
323323
if (!this._handle) {
324324
this.once('connect',
325-
enable ? this.setNoDelay : this.setNoDelay.bind(this, enable));
325+
enable ? this.setNoDelay : () => this.setNoDelay(enable));
326326
return this;
327327
}
328328

@@ -336,7 +336,7 @@ Socket.prototype.setNoDelay = function(enable) {
336336

337337
Socket.prototype.setKeepAlive = function(setting, msecs) {
338338
if (!this._handle) {
339-
this.once('connect', this.setKeepAlive.bind(this, setting, msecs));
339+
this.once('connect', () => this.setKeepAlive(setting, msecs));
340340
return this;
341341
}
342342

@@ -384,7 +384,7 @@ Socket.prototype._read = function(n) {
384384

385385
if (this._connecting || !this._handle) {
386386
debug('_read wait for connection');
387-
this.once('connect', this._read.bind(this, n));
387+
this.once('connect', () => this._read(n));
388388
} else if (!this._handle.reading) {
389389
// not already reading, start the flow
390390
debug('Socket._read readStart');

0 commit comments

Comments
 (0)