Skip to content

Commit 9c842f4

Browse files
apapirovskitargos
authored andcommitted
lib: remove Reflect.apply where appropriate
Using Reflect.apply where the callback context does not need to change is unnecessary and less performant. PR-URL: #27349 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 8495e8b commit 9c842f4

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
4+
const bench = common.createBenchmark(main, {
5+
n: [1e6],
6+
});
7+
8+
function main({ n }) {
9+
var j = 0;
10+
function cb1(arg1) {
11+
j++;
12+
if (j === n)
13+
bench.end(n);
14+
}
15+
function cb2(arg1, arg2) {
16+
j++;
17+
if (j === n)
18+
bench.end(n);
19+
}
20+
function cb3(arg1, arg2, arg3) {
21+
j++;
22+
if (j === n)
23+
bench.end(n);
24+
}
25+
function cb4(arg1, arg2, arg3, arg4) {
26+
j++;
27+
if (j === n)
28+
bench.end(n);
29+
}
30+
31+
bench.start();
32+
for (var i = 0; i < n; i++) {
33+
if (i % 4 === 0)
34+
setTimeout(cb4, 1, 3.14, 1024, true, false);
35+
else if (i % 3 === 0)
36+
setTimeout(cb3, 1, 512, true, null);
37+
else if (i % 2 === 0)
38+
setTimeout(cb2, 1, false, 5.1);
39+
else
40+
setTimeout(cb1, 1, 0);
41+
}
42+
}

lib/fs.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
'use strict';
2626

27-
const { Math, Object, Reflect } = primordials;
27+
const { Math, Object } = primordials;
2828

2929
const { fs: constants } = internalBinding('constants');
3030
const {
@@ -133,9 +133,7 @@ function makeCallback(cb) {
133133
throw new ERR_INVALID_CALLBACK(cb);
134134
}
135135

136-
return (...args) => {
137-
return Reflect.apply(cb, undefined, args);
138-
};
136+
return (...args) => cb(...args);
139137
}
140138

141139
// Special case of `makeCallback()` that is specific to async `*stat()` calls as

lib/internal/async_hooks.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { FunctionPrototype, Object, Reflect } = primordials;
3+
const { FunctionPrototype, Object } = primordials;
44

55
const {
66
ERR_ASYNC_TYPE,
@@ -278,14 +278,14 @@ function clearDefaultTriggerAsyncId() {
278278

279279
function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
280280
if (triggerAsyncId === undefined)
281-
return Reflect.apply(block, null, args);
281+
return block(...args);
282282
// CHECK(Number.isSafeInteger(triggerAsyncId))
283283
// CHECK(triggerAsyncId > 0)
284284
const oldDefaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];
285285
async_id_fields[kDefaultTriggerAsyncId] = triggerAsyncId;
286286

287287
try {
288-
return Reflect.apply(block, null, args);
288+
return block(...args);
289289
} finally {
290290
async_id_fields[kDefaultTriggerAsyncId] = oldDefaultTriggerAsyncId;
291291
}

lib/internal/process/task_queues.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { FunctionPrototype, Reflect } = primordials;
3+
const { FunctionPrototype } = primordials;
44

55
const {
66
// For easy access to the nextTick state in the C++ land,
@@ -81,7 +81,7 @@ function processTicksAndRejections() {
8181
if (tock.args === undefined)
8282
callback();
8383
else
84-
Reflect.apply(callback, undefined, tock.args);
84+
callback(...tock.args);
8585

8686
emitAfter(asyncId);
8787
}

lib/internal/timers.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
// timers within (or creation of a new list). However, these operations combined
7373
// have shown to be trivial in comparison to other timers architectures.
7474

75-
const { Math, Object, Reflect } = primordials;
75+
const { Math, Object } = primordials;
7676

7777
const {
7878
scheduleTimer,
@@ -438,7 +438,7 @@ function getTimerCallbacks(runNextTicks) {
438438
if (!argv)
439439
immediate._onImmediate();
440440
else
441-
Reflect.apply(immediate._onImmediate, immediate, argv);
441+
immediate._onImmediate(...argv);
442442
} finally {
443443
immediate._onImmediate = null;
444444

@@ -530,7 +530,7 @@ function getTimerCallbacks(runNextTicks) {
530530
if (args === undefined)
531531
timer._onTimeout();
532532
else
533-
Reflect.apply(timer._onTimeout, timer, args);
533+
timer._onTimeout(...args);
534534
} finally {
535535
if (timer._repeat && timer._idleTimeout !== -1) {
536536
timer._idleTimeout = timer._repeat;

0 commit comments

Comments
 (0)