Repository navigation
Description
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 callWhat 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/core7.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
cschanhniem commentedon Jun 18, 2026
Good catch on this. The module-level cache approach is the right fix —
require()goes throughModule._resolveFilename→Module._resolveLookupPathseven when the module is cached, which does fs operations.The cleanest fix would be a module-level variable:
Or even simpler: hoist the require to the module scope with a lazy pattern:
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.