Skip to content

feat: warn when recompute is cheaper than cache retrieval#28

Open
raminjafary wants to merge 1 commit into
unjs:mainfrom
raminjafary:feat/warn-when-slower
Open

feat: warn when recompute is cheaper than cache retrieval#28
raminjafary wants to merge 1 commit into
unjs:mainfrom
raminjafary:feat/warn-when-slower

Conversation

@raminjafary

@raminjafary raminjafary commented Jul 2, 2026

Copy link
Copy Markdown

Adds an opt-in warnWhenSlower option to CacheOptions. When enabled, ocache
times cache retrieval (a hit get) against recompute (the resolver on a miss/
refresh) and emits a one-time console.warn when recomputing the value is
cheaper than reading it back from the cache — i.e. caching is adding net
overhead for that cache.

  • Off by default; only tracks when the option is set, so no cost for existing users.
  • Measured directly around the resolver and storage read (no function wrapping),
    so integrity hashing is unaffected. Handles request dedup and SWR refreshes.
  • Works for both defineCachedFunction and defineCachedHandler.

Addresses nitrojs/nitro#2559/nitrojs/nitro#4401 (Nitro enables it in dev via import.meta.dev).

Usage:

const getUser = defineCachedFunction(fetchUser, { warnWhenSlower: true })

Add an opt-in `warnWhenSlower` option that times cache retrieval vs.
recompute and emits a one-time console.warn when retrieval is non-trivial
and recomputing the value is meaningfully cheaper (i.e. caching adds net
overhead). Requires retrieval above a small floor and a clear margin over
recompute, so fast/local storage and sub-millisecond noise never trigger it.
Off by default; negligible cost when disabled.
@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a811f05f-2a96-4aef-a621-6c088e1b9389

📥 Commits

Reviewing files that changed from the base of the PR and between b0339a2 and 6f69741.

📒 Files selected for processing (4)
  • README.md
  • src/cache.ts
  • src/types.ts
  • test/index.test.ts
👮 Files not reviewed due to content moderation or server errors (4)
  • src/types.ts
  • test/index.test.ts
  • README.md
  • src/cache.ts

📝 Walkthrough

[!WARNING]

Walkthrough skipped

File diffs could not be summarized.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

1 participant