Skip to content

timers: optimize timer functions with improved argument handling #57072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 9 additions & 83 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
'use strict';

const {
ArrayPrototypePush,
MathTrunc,
ObjectDefineProperties,
ObjectDefineProperty,
Expand Down Expand Up @@ -113,38 +112,13 @@ function unenroll(item) {
* after `after` milliseconds.
* @param {Function} callback
* @param {number} [after]
* @param {any} [arg1]
* @param {any} [arg2]
* @param {any} [arg3]
* @param {...any} [args]
* @returns {Timeout}
*/
function setTimeout(callback, after, arg1, arg2, arg3) {
function setTimeout(callback, after, ...args) {
validateFunction(callback, 'callback');

let i, args;
switch (arguments.length) {
// fast cases
case 1:
case 2:
break;
case 3:
args = [arg1];
break;
case 4:
args = [arg1, arg2];
break;
default:
args = [arg1, arg2, arg3];
for (i = 5; i < arguments.length; i++) {
// Extend array dynamically, makes .apply run much faster in v6.0.0
ArrayPrototypePush(args, arguments[i]);
}
break;
}

const timeout = new Timeout(callback, after, args, false, true);
const timeout = new Timeout(callback, after, args.length ? args : undefined, false, true);
insert(timeout, timeout._idleTimeout);

return timeout;
}

Expand Down Expand Up @@ -182,38 +156,13 @@ function clearTimeout(timer) {
* every `repeat` milliseconds.
* @param {Function} callback
* @param {number} [repeat]
* @param {any} [arg1]
* @param {any} [arg2]
* @param {any} [arg3]
* @param {...any} [args]
* @returns {Timeout}
*/
function setInterval(callback, repeat, arg1, arg2, arg3) {
function setInterval(callback, repeat, ...args) {
validateFunction(callback, 'callback');

let i, args;
switch (arguments.length) {
// fast cases
case 1:
case 2:
break;
case 3:
args = [arg1];
break;
case 4:
args = [arg1, arg2];
break;
default:
args = [arg1, arg2, arg3];
for (i = 5; i < arguments.length; i++) {
// Extend array dynamically, makes .apply run much faster in v6.0.0
ArrayPrototypePush(args, arguments[i]);
}
break;
}

const timeout = new Timeout(callback, repeat, args, true, true);
const timeout = new Timeout(callback, repeat, args.length ? args : undefined, true, true);
insert(timeout, timeout._idleTimeout);

return timeout;
}

Expand Down Expand Up @@ -255,35 +204,12 @@ Timeout.prototype[SymbolToPrimitive] = function() {
* Schedules the immediate execution of `callback`
* after I/O events' callbacks.
* @param {Function} callback
* @param {any} [arg1]
* @param {any} [arg2]
* @param {any} [arg3]
* @param {...any} [args]
* @returns {Immediate}
*/
function setImmediate(callback, arg1, arg2, arg3) {
function setImmediate(callback, ...args) {
validateFunction(callback, 'callback');

let i, args;
switch (arguments.length) {
// fast cases
case 1:
break;
case 2:
args = [arg1];
break;
case 3:
args = [arg1, arg2];
break;
default:
args = [arg1, arg2, arg3];
for (i = 4; i < arguments.length; i++) {
// Extend array dynamically, makes .apply run much faster in v6.0.0
ArrayPrototypePush(args, arguments[i]);
}
break;
}

return new Immediate(callback, args);
return new Immediate(callback, args.length ? args : undefined);
}

ObjectDefineProperty(setImmediate, customPromisify, {
Expand Down
Loading