Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f0cf7d8

Browse files
bsnoteMyles Borins
authored and
Myles Borins
committedDec 29, 2015
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 6d6e63c commit f0cf7d8

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
@@ -192,21 +192,21 @@ exports.setTimeout = function(callback, after) {
192192
case 2:
193193
break;
194194
case 3:
195-
ontimeout = callback.bind(timer, arguments[2]);
195+
ontimeout = () => callback.call(timer, arguments[2]);
196196
break;
197197
case 4:
198-
ontimeout = callback.bind(timer, arguments[2], arguments[3]);
198+
ontimeout = () => callback.call(timer, arguments[2], arguments[3]);
199199
break;
200200
case 5:
201201
ontimeout =
202-
callback.bind(timer, arguments[2], arguments[3], arguments[4]);
202+
() => callback.call(timer, arguments[2], arguments[3], arguments[4]);
203203
break;
204204
// slow case
205205
default:
206206
var args = new Array(length - 2);
207207
for (var i = 2; i < length; i++)
208208
args[i - 2] = arguments[i];
209-
ontimeout = callback.apply.bind(callback, timer, args);
209+
ontimeout = () => callback.apply(timer, args);
210210
break;
211211
}
212212
timer._onTimeout = ontimeout;
@@ -247,20 +247,20 @@ exports.setInterval = function(callback, repeat) {
247247
case 2:
248248
break;
249249
case 3:
250-
ontimeout = callback.bind(timer, arguments[2]);
250+
ontimeout = () => callback.call(timer, arguments[2]);
251251
break;
252252
case 4:
253-
ontimeout = callback.bind(timer, arguments[2], arguments[3]);
253+
ontimeout = () => callback.call(timer, arguments[2], arguments[3]);
254254
break;
255255
case 5:
256256
ontimeout =
257-
callback.bind(timer, arguments[2], arguments[3], arguments[4]);
257+
() => callback.call(timer, arguments[2], arguments[3], arguments[4]);
258258
break;
259259
default:
260260
var args = new Array(length - 2);
261261
for (var i = 2; i < length; i += 1)
262262
args[i - 2] = arguments[i];
263-
ontimeout = callback.apply.bind(callback, timer, args);
263+
ontimeout = () => callback.apply(timer, args);
264264
break;
265265
}
266266
timer._onTimeout = wrapper;
@@ -272,7 +272,7 @@ exports.setInterval = function(callback, repeat) {
272272
return timer;
273273

274274
function wrapper() {
275-
timer._repeat.call(this);
275+
timer._repeat();
276276

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

0 commit comments

Comments
 (0)
Please sign in to comment.