Aalexbit-codemod

n-plus-one-loop-call-miner

Read-only Insights miner for N+1 queries and external calls inside loops

analysisinsightsmetricsperformancen-plus-one
Public
4 executions

Run locally

npx codemod n-plus-one-loop-call-miner

n-plus-one-loop-call-miner

Read-only Insights package for detecting likely N+1 queries and external/service calls that happen inside loops.

What It Does

  • Scans js, jsx, ts, tsx, mjs, cjs, mts, and cts files.
  • Detects risky call sites inside statement loops and callback-based iteration.
  • Emits metrics only. It does not rewrite source files.

Detection Scope

Loop shapes:

  • for, for...of, for...in, while, do...while
  • map, forEach, filter, reduce, some, every, flatMap
  • Promise.all(items.map(...))
  • Promise.allSettled(items.map(...))

Risky call kinds:

  • Query-style calls such as findMany, query, execute, selectFrom
  • Repository/data-access calls on repository, repo, dao, store
  • Network calls such as fetch, axios.*, request.*
  • Service/client calls on names containing Client, Service, Api, Reader, Catalog, Discovery
  • Cloud/client SDK calls such as eksClient.describeCluster(...)
  • Lower-confidence generic async calls when they are awaited inside loops or run inside async iteration callbacks

Workflow

Workflow step name:

  • Scan JS/TS for N+1 and loop calls (read-only)

This package has no workflow parameters and no AI cleanup step because the spec requires a read-only analysis workflow.

Metric

Metric emitted:

  • loop-call-risk

Metric cardinalities:

  • riskKind: n_plus_one_query, external_call_in_loop, service_call_in_loop, parallelized_loop_call
  • severity: high, medium, low
  • loopKind: for, for_of, for_in, while, do_while, map, forEach, filter, reduce, some, every, flatMap, promise_all_map, promise_allsettled_map
  • executionStyle: serial, parallel, callback, unknown
  • callKind: query, network, service, client_sdk, repository, generic_async_call
  • callee: normalized callee text such as catalogClient.addLocation, eksClient.describeCluster, fetch, repository.findByPk
  • callFamily: stable bucket such as catalog, discovery, reader, aws_sdk, gcp_sdk, repository, orm, http, generic_client, generic_async

Insights Query Example

ts

File-count query example:

ts

Development

bash

The JSSG fixture harness verifies the read-only behavior and example coverage. The standard fixture harness does not expose metric assertions directly, so metric validation is covered here by package validation plus the codemod implementation using useMetricAtom("loop-call-risk") with the documented cardinalities.

License

MIT

Ready to contribute?

Build your own codemod and share it with the community.