Skip to content

use string search rather than regexp to find moduleid in defs - #349

Merged
iambumblehead merged 1 commit into
iambumblehead:mainfrom
ekremney:use-string-search-for-moduleid
Oct 15, 2026
Merged

iambumblehead merged 1 commit into
iambumblehead:mainfrom
ekremney:use-string-search-for-moduleid

Conversation

@ekremney

Copy link
Copy Markdown
Contributor

Resolves #348.

The loader's resolve hook looked up each import's mock definition by building a regexp from the module url, .*(<url>(\?<treeid>(?:(?!#-#).)*)).*, and running it against defs / gdefs. This replaces that with a literal search that returns the same result:

const moduleIdFind = (moduleid, treeid, defs) => {
  const start = typeof defs === 'string'
    ? defs.lastIndexOf(`${moduleid}?${treeid}`) : -1
  if (start === -1)
    return false

  const end = defs.indexOf('#-#', start)
  return end === -1 ? defs.slice(start) : defs.slice(start, end)
}
  • lastIndexOf matches the last occurrence, as the greedy leading .* did, and the result stops at the next #-#, as (?:(?!#-#).)* did.
  • The isfound=false branch used capture group 2, the part after the url, so it now slices the url prefix off the result.
  • moduleIdEsc (the + escaping from escape + char at regexp to resolve pnpn issue #315) is removed: nothing is interpreted as regexp syntax any more.
  • Applied to both esmockLoader.js and esmockLoader.deprecated.js.

Why

Performance. The unanchored leading .* makes every failed lookup quadratic in the length of defs, and most lookups fail because most imports aren't mocked. defs grows with every mocked module and its export names, so esmock() calls get quadratically slower as the mock map grows. Benchmark from #348 (warm esmock() calls; the target imports N mocked modules with 30 exports each, plus 20 unmocked modules; Node 24.16, macOS arm64):

Mocked modules main (2.7.9) This branch
10 243 ms / call 3.9 ms / call
20 945 ms / call 7.6 ms / call
40 3,625 ms / call 15.2 ms / call

In a ~25k-test suite this took total unit-test time from ~32 min to ~6.5 min.

Correctness. Only + was escaped, so ( and ) in a module path were read as regexp groups. The mock was never matched and the real module was used silently. That affects, for example, Next.js route-group folders like app/(marketing)/.

Tests

  • New tests-node test: "should mock file that has parentheses in path", with fixtures tests/local/(parens) in path/parens-file.js and tests/local/usesParensPath.js. It fails on main and passes here.
  • npm run lint: passes.
  • npm run test:all (Node 24.16): passes.
  • tests-pnpm (pnpm i && npm run test): passes. That's the scoped-package case behind the + escaping.

Unrelated, in case it's useful: on Node 24, node --loader=esmock --test in tests-node has 5 failing tests on main as well. They are "should import mock objects, local / global", "should mock fetch as shown in README", "should mock files with hashbangs" and "should work when modules have CJS imports". They're hidden because test:default falls back to test:current via ||. This branch fails the same 5 and no others.

I left CHANGELOG.md alone, since you write the entries at release time.

a regexp built per import from the module url was quadratic for failed lookups and treated '(' and ')' in paths as regexp syntax, see iambumblehead#348
@iambumblehead

Copy link
Copy Markdown
Owner

@ekremney this looks like a great change. thanks for submitting this

@iambumblehead
iambumblehead merged commit be5ff96 into iambumblehead:main Oct 15, 2026
9 checks passed
@iambumblehead

Copy link
Copy Markdown
Owner

@ekremney
ekremney deleted the use-string-search-for-moduleid branch October 15, 2026 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Performance: mock lookup regex makes esmock() quadratic in mock-map size (3.6 s per call with 40 mocks); proposal to drop the regex

2 participants