Skip to content

Commit ae5bcf9

Browse files
JungMinuminwoojung
authored and
minwoojung
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 93d6b5f commit ae5bcf9

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
@@ -160,7 +160,7 @@ function Client() {
160160
protocol.execute(d);
161161
});
162162

163-
protocol.onResponse = this._onResponse.bind(this);
163+
protocol.onResponse = (res) => this._onResponse(res);
164164
}
165165
inherits(Client, net.Socket);
166166
exports.Client = Client;
@@ -735,7 +735,7 @@ function Interface(stdin, stdout, args) {
735735
prompt: 'debug> ',
736736
input: this.stdin,
737737
output: this.stdout,
738-
eval: this.controlEval.bind(this),
738+
eval: (code, ctx, file, cb) => this.controlEval(code, ctx, file, cb),
739739
useGlobal: false,
740740
ignoreUndefined: true
741741
};
@@ -766,7 +766,7 @@ function Interface(stdin, stdout, args) {
766766
});
767767

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

@@ -1588,7 +1588,8 @@ Interface.prototype.repl = function() {
15881588
this.repl.on('exit', exitDebugRepl);
15891589

15901590
// Set new
1591-
this.repl.eval = this.debugEval.bind(this);
1591+
this.repl.eval = (code, ctx, file, cb) =>
1592+
this.debugEval(code, ctx, file, cb);
15921593
this.repl.context = {};
15931594

15941595
// Swap history
@@ -1603,7 +1604,8 @@ Interface.prototype.repl = function() {
16031604
// Exit debug repl
16041605
Interface.prototype.exitRepl = function() {
16051606
// Restore eval
1606-
this.repl.eval = this.controlEval.bind(this);
1607+
this.repl.eval = (code, ctx, file, cb) =>
1608+
this.controlEval(code, ctx, file, cb);
16071609

16081610
// Swap history
16091611
this.history.debug = this.repl.rli.history;
@@ -1690,8 +1692,8 @@ Interface.prototype.trySpawn = function(cb) {
16901692
// pipe stream into debugger
16911693
this.child = spawn(process.execPath, childArgs);
16921694

1693-
this.child.stdout.on('data', this.childPrint.bind(this));
1694-
this.child.stderr.on('data', this.childPrint.bind(this));
1695+
this.child.stdout.on('data', (text) => this.childPrint(text));
1696+
this.child.stderr.on('data', (text) => this.childPrint(text));
16951697
}
16961698

16971699
this.pause();

lib/_tls_legacy.js

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

706706
if (this._isServer) {
707-
this.ssl.onhandshakestart = onhandshakestart.bind(this);
708-
this.ssl.onhandshakedone = onhandshakedone.bind(this);
709-
this.ssl.onclienthello = onclienthello.bind(this);
710-
this.ssl.onnewsession = onnewsession.bind(this);
707+
this.ssl.onhandshakestart = () => onhandshakestart.call(this);
708+
this.ssl.onhandshakedone = () => onhandshakedone.call(this);
709+
this.ssl.onclienthello = (hello) => onclienthello.call(this, hello);
710+
this.ssl.onnewsession =
711+
(key, session) => onnewsession.call(this, key, session);
711712
this.ssl.lastHandshakeTime = 0;
712713
this.ssl.handshakes = 0;
713714
} else {
714-
this.ssl.onocspresponse = onocspresponse.bind(this);
715+
this.ssl.onocspresponse = (resp) => onocspresponse.call(this, resp);
715716
}
716717

717718
if (process.features.tls_sni) {

lib/_tls_wrap.js

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

394394
if (options.isServer) {
395-
ssl.onhandshakestart = onhandshakestart.bind(this);
396-
ssl.onhandshakedone = onhandshakedone.bind(this);
397-
ssl.onclienthello = onclienthello.bind(this);
398-
ssl.oncertcb = oncertcb.bind(this);
399-
ssl.onnewsession = onnewsession.bind(this);
395+
ssl.onhandshakestart = () => onhandshakestart.call(this);
396+
ssl.onhandshakedone = () => onhandshakedone.call(this);
397+
ssl.onclienthello = (hello) => onclienthello.call(this, hello);
398+
ssl.oncertcb = (info) => oncertcb.call(this, info);
399+
ssl.onnewsession = (key, session) => onnewsession.call(this, key, session);
400400
ssl.lastHandshakeTime = 0;
401401
ssl.handshakes = 0;
402402

@@ -410,8 +410,8 @@ TLSSocket.prototype._init = function(socket, wrap) {
410410
}
411411
} else {
412412
ssl.onhandshakestart = function() {};
413-
ssl.onhandshakedone = this._finishInit.bind(this);
414-
ssl.onocspresponse = onocspresponse.bind(this);
413+
ssl.onhandshakedone = () => this._finishInit();
414+
ssl.onocspresponse = (resp) => onocspresponse.call(this, resp);
415415

416416
if (options.session)
417417
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
@@ -1842,7 +1842,7 @@ ReadStream.prototype.close = function(cb) {
18421842
this.once('open', close);
18431843
return;
18441844
}
1845-
return process.nextTick(this.emit.bind(this, 'close'));
1845+
return process.nextTick(() => this.emit('close'));
18461846
}
18471847
this.closed = true;
18481848
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)