Skip to content

Commit d88d4a2

Browse files
committed
process: use a class for nextTick resources
The resource created for each `process.nextTick()` call was an object literal with three computed symbol keys. V8 builds such literals from a boilerplate that only covers the static keys, so the symbol-keyed properties end up in a separate property array allocated per tick, and each computed-key store goes through the runtime. Construct the resource through a class instead, so that every tick object shares one map with all five fields in-object. process/next-tick-breadth-args.js: ~1.58M -> ~1.95M ops/s (+23%). Signed-off-by: Matteo Collina <hello@matteocollina.com>
1 parent 8bf7793 commit d88d4a2

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

‎lib/internal/process/task_queues.js‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ function processTicksAndRejections() {
110110

111111
// `nextTick()` will not enqueue any callback when the process is about to
112112
// exit since the callback would not have a chance to be executed.
113+
// The async resource behind each `process.nextTick()` call. A class keeps
114+
// every instance on one map with all fields in-object; an object literal
115+
// with computed symbol keys spills them into a separate property array.
116+
class TickObject {
117+
constructor(callback, args, asyncId, triggerAsyncId) {
118+
this[async_id_symbol] = asyncId;
119+
this[trigger_async_id_symbol] = triggerAsyncId;
120+
this[async_context_frame] = AsyncContextFrame.current();
121+
this.callback = callback;
122+
this.args = args;
123+
}
124+
}
125+
113126
function nextTick(callback) {
114127
validateFunction(callback, 'callback');
115128

@@ -132,13 +145,7 @@ function nextTick(callback) {
132145
setHasTickScheduled(true);
133146
const asyncId = newAsyncId();
134147
const triggerAsyncId = getDefaultTriggerAsyncId();
135-
const tickObject = {
136-
[async_id_symbol]: asyncId,
137-
[trigger_async_id_symbol]: triggerAsyncId,
138-
[async_context_frame]: AsyncContextFrame.current(),
139-
callback,
140-
args,
141-
};
148+
const tickObject = new TickObject(callback, args, asyncId, triggerAsyncId);
142149
if (initHooksExist())
143150
emitInit(asyncId, 'TickObject', triggerAsyncId, tickObject);
144151
queue.push(tickObject);

0 commit comments

Comments
 (0)