nodejs / nodejs/require-in-the-middle

Perf impact on module load seems to be depending on the number of hooks defined.

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

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
186
Forks
34
PR merge metrics
No merged PRs in 30d

Description

During our investigations trying to optimize Kibana's startup time, we discovered that the perf impact of using require-in-the-middle was depending on the number of Hook that were defined. (More details in https://github.com/elastic/kibana/pull/178285, but we're talking about ~20% performance gain in total module load time by using only one hook instead of 5)

The implementation of RITM seems to confirm it, given each instance of Hook is patching Module.prototype.require, therefor patching the already-patched require from the previous Hook.

I would expect the library to perform the patching only once, and handle all the registered hooks from this single patched method. It would avoid the interception logic to be executed each time for each hook, for each import.

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 tracing the Hook implementation and its patching of Module.prototype.require. Measure module-load behavior with multiple hooks, then verify that patching occurs once while all registered hooks still run. Done means the performance impact no longer scales with the number of hooks without changing hook behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.