Skip to content

Commit afd1bf6

Browse files
committed
lib: use indexed appends for dependent signals
Avoid a primordial push call for each dependent signal. Signed-off-by: Roberto Bianchi <roberto.bianchi@spendesk.com>
1 parent 47438d9 commit afd1bf6

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
const assert = require('node:assert');
4+
5+
const bench = common.createBenchmark(main, {
6+
n: [1000],
7+
fanout: [0, 1, 16, 64],
8+
});
9+
10+
function main({ n, fanout }) {
11+
const reason = new Error('reason');
12+
const controllers = [];
13+
const signals = [];
14+
15+
for (let i = 0; i < n; i++) {
16+
const controller = new AbortController();
17+
controllers[i] = controller;
18+
for (let j = 0; j < fanout; j++) {
19+
signals[signals.length] = AbortSignal.any([controller.signal]);
20+
}
21+
}
22+
23+
bench.start();
24+
for (const controller of controllers) {
25+
controller.abort(reason);
26+
}
27+
bench.end(n);
28+
29+
assert.strictEqual(signals.length, n * fanout);
30+
assert(signals.every((signal) => signal.aborted));
31+
}

‎lib/internal/abort_controller.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// in https://github.com/mysticatea/abort-controller (MIT license)
55

66
const {
7-
ArrayPrototypePush,
87
FunctionPrototypeCall,
98
ObjectAssign,
109
ObjectDefineProperties,
@@ -577,7 +576,7 @@ function abortSignal(signal, reason) {
577576
dependentSignal[kReason] = reason;
578577
dependentSignal[kAborted] = true;
579578
// 2. Append dependentSignal to dependentSignalsToAbort.
580-
ArrayPrototypePush(dependentSignalsToAbort, dependentSignal);
579+
dependentSignalsToAbort[dependentSignalsToAbort.length] = dependentSignal;
581580
}
582581
});
583582
}

0 commit comments

Comments
 (0)