Skip to content

refactor(driver): replace Date.now() with performance.now() in query - #12756

Closed
antoni306 wants to merge 1 commit into
typeorm:masterfrom
antoni306:refactor/performance-now-query-timing
Closed

antoni306 wants to merge 1 commit into
typeorm:masterfrom
antoni306:refactor/performance-now-query-timing

Conversation

@antoni306

@antoni306 antoni306 commented Aug 5, 2026 •

Copy link
Copy Markdown

…execution timing

Replace Date.now() with performance.now() when measuring query execution time across QueryRunner implementations. performance.now() provides a monotonic, higher-resolution timer better suited for measuring elapsed durations.

Also updated Spanner's updateDDL() method for consistency, since it
follows the same timing pattern as query() within the same driver.

Closes #12731

Description of change

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • This pull request links a relevant issue using a closing keyword:
    Fixes #NNNN, Closes #NNNN, or Resolves #NNNN
  • There are new or updated tests validating the change (tests/**.test.ts)
  • Documentation has been updated to reflect this change (docs/docs/**.md)

🛡️ Security fixes: Do not submit security fixes as public PRs — the diff exposes the vulnerability. Use GitHub Security Advisories instead.

…execution timing

Replace Date.now() with performance.now() when measuring query
execution time across QueryRunner implementations. performance.now()
provides a monotonic, higher-resolution timer better suited for
measuring elapsed durations.

Closes #12731
@github-actions github-actions Bot added linked-issue PR references an issue possible-duplicate PR may duplicate an existing open PR labels Aug 5, 2026
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

Other open PRs also reference #12731: #12750. Maintainers may want to coordinate.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (2) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Zero start time skipped 🐞 Bug ≡ Correctness
Description
In SqlServerQueryRunner's catch block, queryStartTime ? ... : undefined treats a valid
queryStartTime === 0 (possible with performance.now()) as “unset”, so failed queries lose
execution time measurement and may skip slow-query logging on errors.
Code

src/driver/sqlserver/SqlServerQueryRunner.ts[R315-317]

            const queryExecutionTime = queryStartTime
-                ? Date.now() - queryStartTime
+                ? performance.now() - queryStartTime
                : undefined
Evidence
The file shows queryStartTime is assigned from performance.now(), then the catch block uses a
truthiness guard to decide whether to compute elapsed time; after switching to a timer that can
legally be 0, that guard can incorrectly skip timing.

src/driver/sqlserver/SqlServerQueryRunner.ts[231-270]
src/driver/sqlserver/SqlServerQueryRunner.ts[314-318]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`SqlServerQueryRunner` sets `queryStartTime` using `performance.now()`. In the `catch` block it uses a truthiness check (`queryStartTime ? ... : undefined`), which incorrectly treats `0` as “not set”. This can suppress `queryExecutionTime` calculation for failed queries.

### Issue Context
- `queryStartTime` is declared as `number | undefined` and is assigned from `performance.now()`.
- `performance.now()` can return `0` at the performance time origin.

### Fix Focus Areas
- src/driver/sqlserver/SqlServerQueryRunner.ts[314-318]

### Suggested change
Replace the truthiness check with an explicit undefined check:
- `queryStartTime !== undefined ? performance.now() - queryStartTime : undefined`
(or initialize `queryStartTime` to `undefined` and keep explicit check).

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Unguarded performance.now usage 🐞 Bug ☼ Reliability
Description
Multiple QueryRunner implementations now call performance.now() directly; when executed in a
runtime where the global performance object is missing, this will throw a ReferenceError and
prevent queries from running for those drivers.
Code

src/driver/cordova/CordovaQueryRunner.ts[67]

+        const queryStartTime = performance.now()
Evidence
QueryRunners now directly reference performance.now() (no globalThis.performance guard), and
there is no internal platform abstraction that provides a safe fallback; docs indicate these drivers
are intended for multiple non-Node platforms.

src/driver/cordova/CordovaQueryRunner.ts[61-84]
src/driver/react-native/ReactNativeQueryRunner.ts[64-82]
src/platform/PlatformTools.ts[17-31]
docs/docs/help/2-supported-platforms.md[51-94]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
Several QueryRunners now evaluate `performance.now()` directly. If `performance` is not defined in the executing JS runtime, this becomes a hard runtime failure (ReferenceError) during query execution.

### Issue Context
TypeORM supports non-Node platforms/drivers (Cordova, React Native, NativeScript, etc.), and the codebase does not provide a centralized timing helper/fallback.

### Fix Focus Areas
- src/driver/cordova/CordovaQueryRunner.ts[63-84]
- src/driver/react-native/ReactNativeQueryRunner.ts[64-82]
- src/driver/better-sqlite3/BetterSqlite3QueryRunner.ts[104-140]
- src/driver/nativescript/NativescriptQueryRunner.ts[109-114]
- src/platform/PlatformTools.ts[17-31]

### Suggested change
Introduce a safe `now()` helper (ideally in `PlatformTools`) and use it everywhere timing is needed:
- `const now = () => globalThis.performance?.now?.call(globalThis.performance) ?? Date.now()`
Then replace `performance.now()` calls in QueryRunners with `now()` (or `PlatformTools.now()`), so runtimes without `performance` don’t crash.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


  • Author self-review: I have reviewed the code review findings, and addressed the relevant ones.

To customize comments, go to the Qodo configuration screen, or learn more in the docs.

Qodo Logo

@antoni306 antoni306 changed the title refactor(driver): replace Date.now() with performance.now() in query … refactor(driver): replace Date.now with performance.now in query … Aug 5, 2026
@antoni306 antoni306 changed the title refactor(driver): replace Date.now with performance.now in query … refactor(driver): replace Date.now with performance.now in query Aug 5, 2026
@antoni306 antoni306 changed the title refactor(driver): replace Date.now with performance.now in query refactor(driver): replace Date.now() with performance.now() in query Aug 5, 2026
@alumni
alumni requested a review from Cprakhar August 26, 2026 14:01
@antoni306 antoni306 closed this by deleting the head repository Sep 4, 2026
@alumni

alumni commented Oct 21, 2026

Copy link
Copy Markdown
Collaborator

@antoni306 thanks for opening this PR, we merged another one that is fixing the same issue.

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

Labels

linked-issue PR references an issue possible-duplicate PR may duplicate an existing open PR

Development

Successfully merging this pull request may close these issues.

Replace Date.now() with performance.now() in query() method

3 participants