Skip to content

Commit

Permalink
timers: optimize callback call: bind -> arrow
Browse files Browse the repository at this point in the history
ES6 arrow functions are much more efficient than `.bind()` functions.

PR-URL: #4038
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
  • Loading branch information
bsnote authored and Myles Borins committed Jan 19, 2016
1 parent 6dd375c commit 86b47e8
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,21 @@ exports.setTimeout = function(callback, after) {
case 2:
break;
case 3:
ontimeout = callback.bind(timer, arguments[2]);
ontimeout = () => callback.call(timer, arguments[2]);
break;
case 4:
ontimeout = callback.bind(timer, arguments[2], arguments[3]);
ontimeout = () => callback.call(timer, arguments[2], arguments[3]);
break;
case 5:
ontimeout =
callback.bind(timer, arguments[2], arguments[3], arguments[4]);
() => callback.call(timer, arguments[2], arguments[3], arguments[4]);
break;
// slow case
default:
var args = new Array(length - 2);
for (var i = 2; i < length; i++)
args[i - 2] = arguments[i];
ontimeout = callback.apply.bind(callback, timer, args);
ontimeout = () => callback.apply(timer, args);
break;
}
timer._onTimeout = ontimeout;
Expand Down Expand Up @@ -247,20 +247,20 @@ exports.setInterval = function(callback, repeat) {
case 2:
break;
case 3:
ontimeout = callback.bind(timer, arguments[2]);
ontimeout = () => callback.call(timer, arguments[2]);
break;
case 4:
ontimeout = callback.bind(timer, arguments[2], arguments[3]);
ontimeout = () => callback.call(timer, arguments[2], arguments[3]);
break;
case 5:
ontimeout =
callback.bind(timer, arguments[2], arguments[3], arguments[4]);
() => callback.call(timer, arguments[2], arguments[3], arguments[4]);
break;
default:
var args = new Array(length - 2);
for (var i = 2; i < length; i += 1)
args[i - 2] = arguments[i];
ontimeout = callback.apply.bind(callback, timer, args);
ontimeout = () => callback.apply(timer, args);
break;
}
timer._onTimeout = wrapper;
Expand All @@ -272,7 +272,7 @@ exports.setInterval = function(callback, repeat) {
return timer;

function wrapper() {
timer._repeat.call(this);
timer._repeat();

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

0 comments on commit 86b47e8

Please sign in to comment.