Skip to content

Commit 34961c7

Browse files
committed
process: improve nextTick performance
PR-URL: #25461 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent 0150490 commit 34961c7

8 files changed

+29
-27
lines changed

benchmark/process/next-tick-breadth-args.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const common = require('../common.js');
44
const bench = common.createBenchmark(main, {
5-
n: [4e6]
5+
n: [1e7]
66
});
77

88
function main({ n }) {

benchmark/process/next-tick-breadth.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const common = require('../common.js');
44
const bench = common.createBenchmark(main, {
5-
n: [4e6]
5+
n: [1e7]
66
});
77

88
function main({ n }) {

benchmark/process/next-tick-depth-args.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const common = require('../common.js');
44
const bench = common.createBenchmark(main, {
5-
n: [12e6]
5+
n: [7e6]
66
});
77

88
function main({ n }) {

benchmark/process/next-tick-depth.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const common = require('../common.js');
33
const bench = common.createBenchmark(main, {
4-
n: [12e6]
4+
n: [7e6]
55
});
66

77
function main({ n }) {

benchmark/process/next-tick-exec-args.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const common = require('../common.js');
33
const bench = common.createBenchmark(main, {
4-
n: [5e6]
4+
n: [4e6]
55
});
66

77
function main({ n }) {

benchmark/process/next-tick-exec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
const common = require('../common.js');
33
const bench = common.createBenchmark(main, {
4-
n: [5e6]
4+
n: [4e6]
55
});
66

77
function main({ n }) {

lib/internal/fixed_queue.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ module.exports = class FixedQueue {
102102
}
103103

104104
shift() {
105-
const { tail } = this;
105+
const tail = this.tail;
106106
const next = tail.shift();
107107
if (tail.isEmpty() && tail.next !== null) {
108108
// If there is another queue, it forms the new tail.

lib/internal/process/task_queues.js

+22-20
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,18 @@ function processTicksAndRejections() {
7171

7272
try {
7373
const callback = tock.callback;
74-
if (tock.args === undefined)
74+
if (tock.args === undefined) {
7575
callback();
76-
else
77-
callback(...tock.args);
76+
} else {
77+
const args = tock.args;
78+
switch (args.length) {
79+
case 1: callback(args[0]); break;
80+
case 2: callback(args[0], args[1]); break;
81+
case 3: callback(args[0], args[1], args[2]); break;
82+
case 4: callback(args[0], args[1], args[2], args[3]); break;
83+
default: callback(...args);
84+
}
85+
}
7886
} finally {
7987
if (destroyHooksExist())
8088
emitDestroy(asyncId);
@@ -88,22 +96,6 @@ function processTicksAndRejections() {
8896
setHasRejectionToWarn(false);
8997
}
9098

91-
class TickObject {
92-
constructor(callback, args) {
93-
this.callback = callback;
94-
this.args = args;
95-
96-
const asyncId = newAsyncId();
97-
const triggerAsyncId = getDefaultTriggerAsyncId();
98-
this[async_id_symbol] = asyncId;
99-
this[trigger_async_id_symbol] = triggerAsyncId;
100-
101-
if (initHooksExist()) {
102-
emitInit(asyncId, 'TickObject', triggerAsyncId, this);
103-
}
104-
}
105-
}
106-
10799
// `nextTick()` will not enqueue any callback when the process is about to
108100
// exit since the callback would not have a chance to be executed.
109101
function nextTick(callback) {
@@ -127,7 +119,17 @@ function nextTick(callback) {
127119

128120
if (queue.isEmpty())
129121
setHasTickScheduled(true);
130-
queue.push(new TickObject(callback, args));
122+
const asyncId = newAsyncId();
123+
const triggerAsyncId = getDefaultTriggerAsyncId();
124+
const tickObject = {
125+
[async_id_symbol]: asyncId,
126+
[trigger_async_id_symbol]: triggerAsyncId,
127+
callback,
128+
args
129+
};
130+
if (initHooksExist())
131+
emitInit(asyncId, 'TickObject', triggerAsyncId, tickObject);
132+
queue.push(tickObject);
131133
}
132134

133135
let AsyncResource;

0 commit comments

Comments
 (0)