Skip to content

Commit fbcd687

Browse files
bsnoteFishrock123
authored andcommitted
timers: optimize callback call: bind -> arrow
ES6 arrow functions are much more efficient than `.bind()` functions. PR-URL: #4038 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]>
1 parent 8eb153d commit fbcd687

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

lib/timers.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -193,21 +193,21 @@ exports.setTimeout = function(callback, after) {
193193
case 2:
194194
break;
195195
case 3:
196-
ontimeout = callback.bind(timer, arguments[2]);
196+
ontimeout = () => callback.call(timer, arguments[2]);
197197
break;
198198
case 4:
199-
ontimeout = callback.bind(timer, arguments[2], arguments[3]);
199+
ontimeout = () => callback.call(timer, arguments[2], arguments[3]);
200200
break;
201201
case 5:
202202
ontimeout =
203-
callback.bind(timer, arguments[2], arguments[3], arguments[4]);
203+
() => callback.call(timer, arguments[2], arguments[3], arguments[4]);
204204
break;
205205
// slow case
206206
default:
207207
var args = new Array(length - 2);
208208
for (var i = 2; i < length; i++)
209209
args[i - 2] = arguments[i];
210-
ontimeout = callback.apply.bind(callback, timer, args);
210+
ontimeout = () => callback.apply(timer, args);
211211
break;
212212
}
213213
timer._onTimeout = ontimeout;
@@ -248,20 +248,20 @@ exports.setInterval = function(callback, repeat) {
248248
case 2:
249249
break;
250250
case 3:
251-
ontimeout = callback.bind(timer, arguments[2]);
251+
ontimeout = () => callback.call(timer, arguments[2]);
252252
break;
253253
case 4:
254-
ontimeout = callback.bind(timer, arguments[2], arguments[3]);
254+
ontimeout = () => callback.call(timer, arguments[2], arguments[3]);
255255
break;
256256
case 5:
257257
ontimeout =
258-
callback.bind(timer, arguments[2], arguments[3], arguments[4]);
258+
() => callback.call(timer, arguments[2], arguments[3], arguments[4]);
259259
break;
260260
default:
261261
var args = new Array(length - 2);
262262
for (var i = 2; i < length; i += 1)
263263
args[i - 2] = arguments[i];
264-
ontimeout = callback.apply.bind(callback, timer, args);
264+
ontimeout = () => callback.apply(timer, args);
265265
break;
266266
}
267267
timer._onTimeout = wrapper;
@@ -273,7 +273,7 @@ exports.setInterval = function(callback, repeat) {
273273
return timer;
274274

275275
function wrapper() {
276-
timer._repeat.call(this);
276+
timer._repeat();
277277

278278
// Timer might be closed - no point in restarting it
279279
if (!timer._repeat)

0 commit comments

Comments
 (0)