| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 0457bfe commit 012bf70
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,43 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common.js'); | ||
| 4 | + | ||
| 5 | + // Benchmarks the throughput of processing many promise rejections that are | ||
| 6 | + // initially unhandled, get warned, and then handled asynchronously, exercising | ||
| 7 | + // asyncHandledRejections + processPromiseRejections. | ||
| 8 | + // | ||
| 9 | + // Note: This benchmark uses --unhandled-rejections=warn to avoid crashing | ||
| 10 | + // when promises are temporarily unhandled. | ||
| 11 | + | ||
| 12 | + const bench = common.createBenchmark(main, { | ||
| 13 | + n: [1e4, 5e4, 1e5], | ||
| 14 | + }, { | ||
| 15 | + flags: ['--unhandled-rejections=warn'], | ||
| 16 | + }); | ||
| 17 | + | ||
| 18 | + function main({ n }) { | ||
| 19 | + const rejections = []; | ||
| 20 | + | ||
| 21 | + // Suppress warning output during the benchmark | ||
| 22 | + process.removeAllListeners('warning'); | ||
| 23 | + | ||
| 24 | + for (let i = 0; i < n; i++) { | ||
| 25 | + rejections.push(Promise.reject(i)); | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + // Wait for them to be processed as unhandled and warned. | ||
| 29 | + setImmediate(() => { | ||
| 30 | + setImmediate(() => { | ||
| 31 | + bench.start(); | ||
| 32 | + | ||
| 33 | + for (let i = 0; i < n; i++) { | ||
| 34 | + rejections[i].catch(() => {}); | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + // Let processPromiseRejections drain asyncHandledRejections. | ||
| 38 | + setImmediate(() => { | ||
| 39 | + bench.end(n); | ||
| 40 | + }); | ||
| 41 | + }); | ||
| 42 | + }); | ||
| 43 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,14 +1,14 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | - ArrayPrototypePush, | ||
| 5 | - ArrayPrototypeShift, | ||
| 6 | 4 | Error, | |
| 7 | 5 | ObjectPrototypeHasOwnProperty, | |
| 8 | 6 | SafeMap, | |
| 9 | 7 | SafeWeakMap, | |
| 10 | 8 | } = primordials; | |
| 11 | 9 | ||
| 10 | + const FixedQueue = require('internal/fixed_queue'); | ||
| 11 | + | ||
| 12 | 12 | const { | |
| 13 | 13 | tickInfo, | |
| 14 | 14 | promiseRejectEvents: { | |
@@ -119,9 +119,9 @@ const maybeUnhandledPromises = new SafeWeakMap(); | |||
| 119 | 119 | let pendingUnhandledRejections = new SafeMap(); | |
| 120 | 120 | ||
| 121 | 121 | /** | |
| 122 | - * @type {Array<{promise: Promise, warning: Error}>} | ||
| 122 | + * @type {import('internal/fixed_queue')<{promise: Promise, warning: Error}>} | ||
| 123 | 123 | */ | |
| 124 | - const asyncHandledRejections = []; | ||
| 124 | + const asyncHandledRejections = new FixedQueue(); | ||
| 125 | 125 | ||
| 126 | 126 | /** | |
| 127 | 127 | * @type {number} | |
@@ -219,7 +219,7 @@ function handledRejection(promise) { | |||
| 219 | 219 | if (promiseInfo.warned) { | |
| 220 | 220 | // Generate the warning object early to get a good stack trace. | |
| 221 | 221 | const warning = new PromiseRejectionHandledWarning(promiseInfo.uid); | |
| 222 | - ArrayPrototypePush(asyncHandledRejections, { promise, warning }); | ||
| 222 | + asyncHandledRejections.push({ promise, warning }); | ||
| 223 | 223 | setHasRejectionToWarn(true); | |
| 224 | 224 | } | |
| 225 | 225 | } | |
@@ -375,10 +375,10 @@ function getUnhandledRejectionsMode() { | |||
| 375 | 375 | // a warning to be emitted which requires the microtask and next tick | |
| 376 | 376 | // queues to be drained again. | |
| 377 | 377 | function processPromiseRejections() { | |
| 378 | - let maybeScheduledTicksOrMicrotasks = asyncHandledRejections.length > 0; | ||
| 378 | + let maybeScheduledTicksOrMicrotasks = !asyncHandledRejections.isEmpty(); | ||
| 379 | 379 | ||
| 380 | - while (asyncHandledRejections.length !== 0) { | ||
| 381 | - const { promise, warning } = ArrayPrototypeShift(asyncHandledRejections); | ||
| 380 | + while (!asyncHandledRejections.isEmpty()) { | ||
| 381 | + const { promise, warning } = asyncHandledRejections.shift(); | ||
| 382 | 382 | if (!process.emit('rejectionHandled', promise)) { | |
| 383 | 383 | process.emitWarning(warning); | |
| 384 | 384 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments