From 67d1e1ef513d92d2d800acd2e79fe309a8274ce2 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 1 May 2026 16:34:06 +0200 Subject: [PATCH 1/2] stream: avoid extra promise allocation in `createPromiseCallback` Signed-off-by: Antoine du Hamel --- lib/internal/per_context/primordials.js | 8 ++++++++ lib/internal/webstreams/util.js | 5 +++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index e2b5f763df7dd9..38082b4eb51171 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -243,6 +243,7 @@ function copyPrototype(src, dest, prefix) { copyPrototype(original.prototype, primordials, `${name}Prototype`); }); + // Create copies of abstract intrinsic objects that are not directly exposed // on the global object. // Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object @@ -273,6 +274,7 @@ const { ArrayPrototypePushApply, ArrayPrototypeSlice, FinalizationRegistry, + FunctionPrototypeBind, FunctionPrototypeCall, Map, ObjectDefineProperties, @@ -435,6 +437,12 @@ const SafePromise = makeSafe( class SafePromise extends Promise {}, ); +{ + // eslint-disable-next-line node-core/prefer-primordials + const OriginalPromiseTry = Promise.try; + primordials.BindPromiseTry = (fn) => FunctionPrototypeBind(OriginalPromiseTry, Promise, fn); +} + /** * Attaches a callback that is invoked when the Promise is settled (fulfilled or * rejected). The resolved value cannot be modified from the callback. diff --git a/lib/internal/webstreams/util.js b/lib/internal/webstreams/util.js index 81e65815eabe6b..dabb1de6047616 100644 --- a/lib/internal/webstreams/util.js +++ b/lib/internal/webstreams/util.js @@ -7,10 +7,11 @@ const { ArrayPrototypePush, ArrayPrototypeShift, AsyncIteratorPrototype, + BindPromiseTry, + FunctionPrototypeBind, MathMax, NumberIsNaN, PromisePrototypeThen, - ReflectApply, ReflectGet, Symbol, Uint8Array, @@ -164,7 +165,7 @@ function enqueueValueWithSize(controller, value, size) { function createPromiseCallback(name, fn, thisArg) { validateFunction(fn, name); - return async (...args) => ReflectApply(fn, thisArg, args); + return BindPromiseTry(thisArg === undefined ? fn : FunctionPrototypeBind(fn, thisArg)); } function isPromisePending(promise) { From 55bde300d97f84f41130a08d3dbc3590c36dac94 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Fri, 1 May 2026 20:27:09 +0200 Subject: [PATCH 2/2] fixup! stream: avoid extra promise allocation in `createPromiseCallback` --- lib/internal/per_context/primordials.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/internal/per_context/primordials.js b/lib/internal/per_context/primordials.js index 38082b4eb51171..9044cd1f525d57 100644 --- a/lib/internal/per_context/primordials.js +++ b/lib/internal/per_context/primordials.js @@ -243,7 +243,6 @@ function copyPrototype(src, dest, prefix) { copyPrototype(original.prototype, primordials, `${name}Prototype`); }); - // Create copies of abstract intrinsic objects that are not directly exposed // on the global object. // Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object