fix: replace hrtime with performance not to allocate#1204
Merged
Conversation
Signed-off-by: ferhat elmas <elmas.ferhat@gmail.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors latency measurements across the storage DB layer, queue scheduling, and HTTP metrics to use performance.now() (numeric monotonic timestamps) instead of process.hrtime.bigint(), reducing allocations in hot paths while preserving duration semantics (seconds).
Changes:
- Replace
process.hrtime.bigint()start/end timing withperformance.now()and compute durations in seconds via(now - start) / 1000. - Update type surfaces where timing values are stored (e.g.,
FastifyRequest.metricsStartTimeand DB duration recorder signature) frombiginttonumber. - Add/adjust unit tests to assert metrics are recorded from numeric monotonic timestamps and to fix falsy-check behavior for
0start times.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/storage/database/pg.ts | Switch DB query duration timing to performance.now() and update recorder signature to number. |
| src/storage/database/pg.test.ts | Add deterministic tests for scoped/unscoped DB query duration metrics using mocked performance.now(). |
| src/internal/queue/event.ts | Switch queue scheduling timing to performance.now() and compute duration in seconds. |
| src/internal/queue/event.test.ts | Add test verifying queue scheduling duration metric uses numeric monotonic timestamps. |
| src/http/plugins/metrics.ts | Switch HTTP request timing to performance.now(), fix start-time presence check for 0, and update request augmentation type. |
| src/http/plugins/metrics.test.ts | Add test verifying HTTP metrics use numeric monotonic timestamps and that 0 is handled correctly. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Coverage Report for CI Build 28777683411Coverage decreased (-0.008%) to 78.846%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats💛 - Coveralls |
fenos
approved these changes
Jul 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What kind of change does this PR introduce?
refactor
What is the current behavior?
We use process.hrtime at the moment, which returns bigint. It's heap allocated.
It's not needed because a few seconds later, it's converted to a number for latency metrics.
What is the new behavior?
Use performance.now because it's monotonic, made for this purpose and not heap allocated.
Additional context
This cuts down garbage in hot paths.