Skip to content

Commit 67d1e1e

Browse files
committed
stream: avoid extra promise allocation in createPromiseCallback
Signed-off-by: Antoine du Hamel <[email protected]>
1 parent 66054cc commit 67d1e1e

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

lib/internal/per_context/primordials.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ function copyPrototype(src, dest, prefix) {
243243
copyPrototype(original.prototype, primordials, `${name}Prototype`);
244244
});
245245

246+
246247
// Create copies of abstract intrinsic objects that are not directly exposed
247248
// on the global object.
248249
// Refs: https://tc39.es/ecma262/#sec-%typedarray%-intrinsic-object
@@ -273,6 +274,7 @@ const {
273274
ArrayPrototypePushApply,
274275
ArrayPrototypeSlice,
275276
FinalizationRegistry,
277+
FunctionPrototypeBind,
276278
FunctionPrototypeCall,
277279
Map,
278280
ObjectDefineProperties,
@@ -435,6 +437,12 @@ const SafePromise = makeSafe(
435437
class SafePromise extends Promise {},
436438
);
437439

440+
{
441+
// eslint-disable-next-line node-core/prefer-primordials
442+
const OriginalPromiseTry = Promise.try;
443+
primordials.BindPromiseTry = (fn) => FunctionPrototypeBind(OriginalPromiseTry, Promise, fn);
444+
}
445+
438446
/**
439447
* Attaches a callback that is invoked when the Promise is settled (fulfilled or
440448
* rejected). The resolved value cannot be modified from the callback.

lib/internal/webstreams/util.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ const {
77
ArrayPrototypePush,
88
ArrayPrototypeShift,
99
AsyncIteratorPrototype,
10+
BindPromiseTry,
11+
FunctionPrototypeBind,
1012
MathMax,
1113
NumberIsNaN,
1214
PromisePrototypeThen,
13-
ReflectApply,
1415
ReflectGet,
1516
Symbol,
1617
Uint8Array,
@@ -164,7 +165,7 @@ function enqueueValueWithSize(controller, value, size) {
164165

165166
function createPromiseCallback(name, fn, thisArg) {
166167
validateFunction(fn, name);
167-
return async (...args) => ReflectApply(fn, thisArg, args);
168+
return BindPromiseTry(thisArg === undefined ? fn : FunctionPrototypeBind(fn, thisArg));
168169
}
169170

170171
function isPromisePending(promise) {

0 commit comments

Comments
 (0)