Skip to content

Add fast path for Promise creation - #1387

Merged
saghul merged 1 commit into
masterfrom
promise-fast-path
Mar 7, 2026
Merged

saghul merged 1 commit into
masterfrom
promise-fast-path

Conversation

@saghul

@saghul saghul commented Mar 6, 2026

Copy link
Copy Markdown
Contributor

When this is the builtin Promise constructor, call js_promise_new directly instead of
going through js_new_promise_capability which creates an unnecessary executor intermediary.

@bnoordhuis

Copy link
Copy Markdown
Contributor

Have you checked the spec whether that's sound?

I remember when promises got introduced in node (and bluebird still was a thing), that there are some corner cases where simply replacing globalThis.Promise results in user-observable changes in behavior but I don't remember the details.

test262 results may not be reliable for this; it doesn't appear to have coverage.

@saghul

saghul commented Mar 7, 2026

Copy link
Copy Markdown
Contributor Author

I'll check the spec, I admit I haven't.

Thanks for the insight!

@saghul

saghul commented Mar 7, 2026

Copy link
Copy Markdown
Contributor Author

I couldn't find an explanation in the spec, but since the Promise prototype is not writable and doesn't use @@species, I don't see how this change could be observed.

V8 seems to do the same trick, if I'm reading this correctly: https://github.com/v8/v8/blob/5fe0aa3bc79c0a9d3ad546b79211f07105f09585/src/builtins/promise-resolve.tq#L66

@saghul

saghul commented Mar 7, 2026

Copy link
Copy Markdown
Contributor Author

Looks like V8 also fast-paths .then, if the @@species chain is intact, I'll look into that for a follow-up PR. https://github.com/v8/v8/blob/f9e2258a2c6c7aa50372c496f75db8ddcecff25f/src/builtins/promise-then.tq#L42

@bnoordhuis bnoordhuis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, LGTM then. One final question though: can't you move the this_val == ctx->promise_ctor inside js_new_promise_capability?

@saghul

saghul commented Mar 7, 2026 •

Copy link
Copy Markdown
Contributor Author

I can take a look but I think not because some functions do look at species so we'd also need to check for that.

Nevermind, you're right! It can be made simpler and all call sites for js_new_promise_capability benefit.

When `this` is the builtin Promise constructor, `js_new_promise_capability` will call `js_promise_new`,
which initializes a promise and resolving functions, without the unnecessary executor intermediary.
@saghul
saghul force-pushed the promise-fast-path branch from d8a6ab0 to dc09bf8 Compare March 7, 2026 22:10
@saghul saghul changed the title Add fast path for Promise.{resolve,reject,try,withResolvers} Add fast path for Promise creation Mar 7, 2026
@saghul
saghul merged commit 4951c83 into master Mar 7, 2026
122 checks passed
@saghul
saghul deleted the promise-fast-path branch March 7, 2026 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants