Skip to content

isModelStatic re-runs require('../model') on every call (redundant module resolution on a hot path) #18233

Closed

Description

@Svehla

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

Bug Description

isModelStatic() (in packages/core/src/utils/model-utils.ts) calls require('../model') on every invocation:

export function isModelStatic<M extends Model>(val: any): val is ModelStatic<M> {
  // TODO: temporary workaround due to cyclic import.
  const { Model: TmpModel } = require('../model');

  return typeof val === 'function' && val.prototype instanceof TmpModel;
}

isModelStatic sits on a hot path — it is called (directly and via isSameInitialModel / extractModelDefinition) once per association while building queries. Even though Node caches the loaded module, require() still re-runs Module._load → resolution on every call. Under some Node configurations (custom loaders / strict ESM-CJS interop / exports-based resolution) that resolution is not cheap: it walks trySelf → findParentPackageJSON → fs.stat, and on include-heavy queries this dominated CPU in our production profiles.

In a plain Node profile the cost is smaller, but it is pure waste: the resolved Model class never changes once the module graph is initialized, so re-resolving it on every call buys nothing.

Reproducible Example

This isn't a behavioral bug (results are correct), so a functional SSCCE isn't applicable — it's a redundant-work / performance bug. The redundant work is directly observable by counting how many times the module system is hit:

const Module = require('node:module');
const { Sequelize, isModelStatic } = require('@sequelize/core');
const { SqliteDialect } = require('@sequelize/sqlite3');

const MyModel = new Sequelize({ dialect: SqliteDialect }).define('MyModel', {});
isModelStatic(MyModel); // warm up

const orig = Module._load;
let modelLoads = 0;
Module._load = function (request, ...rest) {
  if (request === '../model' || String(request).endsWith('/model')) modelLoads++;
  return orig.call(this, request, ...rest);
};
for (let i = 0; i < 50; i++) isModelStatic(MyModel);
Module._load = orig;

console.log('Model module resolved', modelLoads, 'times'); // -> 50, once per call

What do you expect to happen?

The Model class should be resolved at most once and then reused, so the loop above resolves the module 0 additional times after warm-up.

What is actually happening?

The module is re-resolved on every isModelStatic call (50 times in the snippet above), adding avoidable module-resolution overhead to query building.

Environment

  • Sequelize version: @sequelize/core 7.0.0-alpha.48 (present since the lazy-require workaround was added; we originally hit it on alpha.47)
  • Node.js version: v25.9.0 (also reproduced on Node 20 LTS)
  • If TypeScript related: N/A
  • Database & Version: dialect-agnostic (reproduced on SQLite / PostgreSQL)
  • Connector library & Version: N/A

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

Activity

  1. added
    pending-approval
    on Jun 5, 2026
  2. cschanhniem commented on Jun 18, 2026

    @cschanhniem

    Good catch on this. The module-level cache approach is the right fix — require() goes through Module._resolveFilename → Module._resolveLookupPaths even when the module is cached, which does fs operations.

    The cleanest fix would be a module-level variable:

    // at module top level, outside the function
    let CachedModel: typeof Model | undefined;
    function getModel(): typeof Model {
      if (!CachedModel) {
        CachedModel = require(../model).Model;
      }
      return CachedModel;
    }

    Or even simpler: hoist the require to the module scope with a lazy pattern:

    // top-level lazy require — resolved once
    const getModel = (() => {
      let model: typeof Model | null = null;
      return () => {
        if (!model) model = require(../model).Model;
        return model;
      };
    })();

    This keeps the cyclic-import workaround intact (lazy evaluation defers the resolution until first call) but eliminates the redundant re-resolution on every subsequent call.

    Would be happy to open a PR with this change if it helps.

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

    pending-approval

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions