Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8f7debc

Browse filesBrowse files
gurgundayaduh95
authored andcommitted
timers: optimize timer functions with improved argument handling
PR-URL: #57072 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Gerhard Stöbich <deb2001-github@yahoo.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 595e9e5 commit 8f7debc
Copy full SHA for 8f7debc

File tree

Expand file treeCollapse file tree

1 file changed

+9
-83
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+9
-83
lines changed
Open diff view settings
Collapse file

‎lib/timers.js‎

Copy file name to clipboardExpand all lines: lib/timers.js
+9-83Lines changed: 9 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
'use strict';
2323

2424
const {
25-
ArrayPrototypePush,
2625
MathTrunc,
2726
ObjectDefineProperties,
2827
ObjectDefineProperty,
@@ -131,38 +130,13 @@ function enroll(item, msecs) {
131130
* after `after` milliseconds.
132131
* @param {Function} callback
133132
* @param {number} [after]
134-
* @param {any} [arg1]
135-
* @param {any} [arg2]
136-
* @param {any} [arg3]
133+
* @param {...any} [args]
137134
* @returns {Timeout}
138135
*/
139-
function setTimeout(callback, after, arg1, arg2, arg3) {
136+
function setTimeout(callback, after, ...args) {
140137
validateFunction(callback, 'callback');
141-
142-
let i, args;
143-
switch (arguments.length) {
144-
// fast cases
145-
case 1:
146-
case 2:
147-
break;
148-
case 3:
149-
args = [arg1];
150-
break;
151-
case 4:
152-
args = [arg1, arg2];
153-
break;
154-
default:
155-
args = [arg1, arg2, arg3];
156-
for (i = 5; i < arguments.length; i++) {
157-
// Extend array dynamically, makes .apply run much faster in v6.0.0
158-
ArrayPrototypePush(args, arguments[i]);
159-
}
160-
break;
161-
}
162-
163-
const timeout = new Timeout(callback, after, args, false, true);
138+
const timeout = new Timeout(callback, after, args.length ? args : undefined, false, true);
164139
insert(timeout, timeout._idleTimeout);
165-
166140
return timeout;
167141
}
168142

@@ -200,38 +174,13 @@ function clearTimeout(timer) {
200174
* every `repeat` milliseconds.
201175
* @param {Function} callback
202176
* @param {number} [repeat]
203-
* @param {any} [arg1]
204-
* @param {any} [arg2]
205-
* @param {any} [arg3]
177+
* @param {...any} [args]
206178
* @returns {Timeout}
207179
*/
208-
function setInterval(callback, repeat, arg1, arg2, arg3) {
180+
function setInterval(callback, repeat, ...args) {
209181
validateFunction(callback, 'callback');
210-
211-
let i, args;
212-
switch (arguments.length) {
213-
// fast cases
214-
case 1:
215-
case 2:
216-
break;
217-
case 3:
218-
args = [arg1];
219-
break;
220-
case 4:
221-
args = [arg1, arg2];
222-
break;
223-
default:
224-
args = [arg1, arg2, arg3];
225-
for (i = 5; i < arguments.length; i++) {
226-
// Extend array dynamically, makes .apply run much faster in v6.0.0
227-
ArrayPrototypePush(args, arguments[i]);
228-
}
229-
break;
230-
}
231-
232-
const timeout = new Timeout(callback, repeat, args, true, true);
182+
const timeout = new Timeout(callback, repeat, args.length ? args : undefined, true, true);
233183
insert(timeout, timeout._idleTimeout);
234-
235184
return timeout;
236185
}
237186

@@ -273,35 +222,12 @@ Timeout.prototype[SymbolToPrimitive] = function() {
273222
* Schedules the immediate execution of `callback`
274223
* after I/O events' callbacks.
275224
* @param {Function} callback
276-
* @param {any} [arg1]
277-
* @param {any} [arg2]
278-
* @param {any} [arg3]
225+
* @param {...any} [args]
279226
* @returns {Immediate}
280227
*/
281-
function setImmediate(callback, arg1, arg2, arg3) {
228+
function setImmediate(callback, ...args) {
282229
validateFunction(callback, 'callback');
283-
284-
let i, args;
285-
switch (arguments.length) {
286-
// fast cases
287-
case 1:
288-
break;
289-
case 2:
290-
args = [arg1];
291-
break;
292-
case 3:
293-
args = [arg1, arg2];
294-
break;
295-
default:
296-
args = [arg1, arg2, arg3];
297-
for (i = 4; i < arguments.length; i++) {
298-
// Extend array dynamically, makes .apply run much faster in v6.0.0
299-
ArrayPrototypePush(args, arguments[i]);
300-
}
301-
break;
302-
}
303-
304-
return new Immediate(callback, args);
230+
return new Immediate(callback, args.length ? args : undefined);
305231
}
306232

307233
ObjectDefineProperty(setImmediate, customPromisify, {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.