From 22e9ae84da807d4144b3d25102bf8e5239380b58 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sat, 7 Nov 2026 20:06:10 +0100 Subject: [PATCH] stream: drop rejection step for sync transforms transformStreamDefaultControllerPerformTransform chained a rejection step onto the result of every transform() call. A non-thenable result cannot reject, so return the shared resolved promise directly and save a promise allocation and a microtask per chunk. The write promise of a transform whose transform() returns a non-thenable now settles one microtask earlier than in the reference implementation. The relative order of all stream events is unchanged. Signed-off-by: Matteo Collina --- lib/internal/webstreams/transformstream.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/internal/webstreams/transformstream.js b/lib/internal/webstreams/transformstream.js index bb1d1aa8396d..2943ce3fe099 100644 --- a/lib/internal/webstreams/transformstream.js +++ b/lib/internal/webstreams/transformstream.js @@ -592,11 +592,11 @@ function transformStreamDefaultControllerError(controller, error) { // Mirrors the reference implementation's // `promiseCall(transformAlgorithm, ...).then(undefined, rejectionSteps)`: -// the returned promise settles one microtask after the (coerced) result -// does, and a rejection errors the transform stream before propagating. -// The raw transform callback plus the shared resolved promise for -// non-thenable results replace the previous async wrapper's two implicit -// promises per chunk. +// a rejection errors the transform stream before propagating. A +// non-thenable result cannot reject, so it maps to the shared resolved +// promise without the rejection step; the write then completes one +// microtask earlier than in the reference implementation, with the +// relative order of all stream events unchanged. function transformStreamDefaultControllerPerformTransform(controller, chunk) { const controllerState = controller[kState]; const transformAlgorithm = controllerState.transformAlgorithm; @@ -612,10 +612,9 @@ function transformStreamDefaultControllerPerformTransform(controller, chunk) { } if (result === null || (typeof result !== 'object' && typeof result !== 'function')) { - result = kResolvedPromise; - } else { - result = PromiseResolve(result); + return kResolvedPromise; } + result = PromiseResolve(result); controllerState.performTransformRejected ??= (error) => { transformStreamError(controller[kState].stream, error); throw error;