sidorares / sidorares/node-mysql2

(QUESTION) Avoidance of anonymous function creation on each Connection.write invocation

Open
#640 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

performance
Dominant language
TypeScript
Stars
4.4k
Forks
680
Avg merge
9h 7m
Merged PRs (30d)
59

Description

https://github.com/sidorares/node-mysql2/blob/4c1a807a5025c7fe332d0b621547e2788b876375/lib/connection.js#L248

We create a new anonymous function for each write operation on Connection (which can be called up to 2 times, when we send header and body for large packets separately), while all it's doing is passing stream error to connection._handleNetworkError.

Could we benefit from lazily binding _handleNetworkError to connection instance (AFAIK, .bind() got significantly faster, at least in recent versions of Node) and alway passing the same bound function on .write() invocations?

I am just guessing here, so may be any benefits are negligible. But if it worth at least researching, I would gladly do it, if it wasn't done earlier.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading lib/connection.js around line 248 and trace how Connection.write passes stream errors to _handleNetworkError. Compare the current callback allocation with lazy binding, then benchmark writes including large packets split into header and body. Done means preserving error behavior while showing a meaningful performance benefit or documenting that the benefit is negligible.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
databases
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.