Browse Source

timers: optimize callback call: bind -> arrow

ES6 arrow functions are much more efficient than `.bind()` functions.

PR-URL: https://github.com/nodejs/node/pull/4038
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
process-exit-stdio-flushing
Andrei Sedoi 9 years ago
committed by Jeremiah Senkpiel
parent
commit
fbcd687c08
  1. 18
      lib/timers.js

18
lib/timers.js

@ -193,21 +193,21 @@ exports.setTimeout = function(callback, after) {
case 2: case 2:
break; break;
case 3: case 3:
ontimeout = callback.bind(timer, arguments[2]); ontimeout = () => callback.call(timer, arguments[2]);
break; break;
case 4: case 4:
ontimeout = callback.bind(timer, arguments[2], arguments[3]); ontimeout = () => callback.call(timer, arguments[2], arguments[3]);
break; break;
case 5: case 5:
ontimeout = ontimeout =
callback.bind(timer, arguments[2], arguments[3], arguments[4]); () => callback.call(timer, arguments[2], arguments[3], arguments[4]);
break; break;
// slow case // slow case
default: default:
var args = new Array(length - 2); var args = new Array(length - 2);
for (var i = 2; i < length; i++) for (var i = 2; i < length; i++)
args[i - 2] = arguments[i]; args[i - 2] = arguments[i];
ontimeout = callback.apply.bind(callback, timer, args); ontimeout = () => callback.apply(timer, args);
break; break;
} }
timer._onTimeout = ontimeout; timer._onTimeout = ontimeout;
@ -248,20 +248,20 @@ exports.setInterval = function(callback, repeat) {
case 2: case 2:
break; break;
case 3: case 3:
ontimeout = callback.bind(timer, arguments[2]); ontimeout = () => callback.call(timer, arguments[2]);
break; break;
case 4: case 4:
ontimeout = callback.bind(timer, arguments[2], arguments[3]); ontimeout = () => callback.call(timer, arguments[2], arguments[3]);
break; break;
case 5: case 5:
ontimeout = ontimeout =
callback.bind(timer, arguments[2], arguments[3], arguments[4]); () => callback.call(timer, arguments[2], arguments[3], arguments[4]);
break; break;
default: default:
var args = new Array(length - 2); var args = new Array(length - 2);
for (var i = 2; i < length; i += 1) for (var i = 2; i < length; i += 1)
args[i - 2] = arguments[i]; args[i - 2] = arguments[i];
ontimeout = callback.apply.bind(callback, timer, args); ontimeout = () => callback.apply(timer, args);
break; break;
} }
timer._onTimeout = wrapper; timer._onTimeout = wrapper;
@ -273,7 +273,7 @@ exports.setInterval = function(callback, repeat) {
return timer; return timer;
function wrapper() { function wrapper() {
timer._repeat.call(this); timer._repeat();
// Timer might be closed - no point in restarting it // Timer might be closed - no point in restarting it
if (!timer._repeat) if (!timer._repeat)

Loading…
Cancel
Save