Skip to content

Avoid constructor source scanning for classes without a base class in @loopback/context #11878

Open

Description

@tonynelson19

Describe the bug

On the first call to describeInjectedArguments() for a constructor, LoopBack calls shouldSkipBaseConstructorInjection(), which reads the class’s source and runs regular expressions to determine whether constructor injections should be inherited.

This work also runs for classes that do not extend another class. For those classes, there is no base constructor to inspect. The metadata cache avoids repeated work for the same class, but applications inspecting many distinct classes still incur this cost during startup.

The proposed change in describeInjectedArguments() is:

if (method === '') {
  if (
    Object.getPrototypeOf(target) !== Function.prototype &&
    shouldSkipBaseConstructorInjection(target)
  ) {
    options.ownMetadataOnly = true;
  }
}

Logs

Additional information

We measured the following application startup timings with @loopback/context 9.0.0, Node.js 22.23.4, and macOS 26.7 on an Apple M4 Pro:

Measurement Original With proposed guard
Constructor inspection 147.9 ms 18.5 ms
Overall component registration 224 ms 98 ms

These are medians of three fresh processes per variant, alternating between variants with other startup settings held constant. Of 3,948 inspected classes, 3,384 had no base class.

The improvement saves approximately 126 ms during registration. Constructor inspection is included in registration, so these savings are not additive or a claim about total application startup time.

Reproduction

https://github.com/tonynelson19/loopback-constructor-repro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions