Skip to content

Make the emit and event names once instead of on every event - #141

Open
nigrosimone wants to merge 1 commit into
brianc:masterfrom
nigrosimone:cached-emit-names
Open

nigrosimone wants to merge 1 commit into
brianc:masterfrom
nigrosimone:cached-emit-names

Conversation

@nigrosimone

Copy link
Copy Markdown

Connection::Emit() creates a new string for the event name and calls runInAsyncScope(handle(), "emit", ...) on every readable or writable event. That overload creates the "emit" string again and looks it up by a name that is not internalized, which costs more than the rest of the call. This makes the three names once per connection, internalized. emit is still looked up on every event, so replacing it keeps working.

A microbenchmark of the same call (a native object with EventEmitter.prototype two levels up, one listener, Node 26): 662 to 366 ns per event. With pg-native, sequential SELECT 1 on a local Postgres, 20 rounds: client CPU per query 56.96 to 55.99 us (-1.8%; the same build against itself 0.997).

The tests pass (84).

@nigrosimone
nigrosimone marked this pull request as ready for review October 4, 2026 15:25
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.

1 participant