Loading skill
Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Never use Date.now() in queries as it breaks caching and reactivity
| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-02 | ✗→✓ | ▲ Improved | 6% | 0% |
| case-09 | ✗→✓ | ▲ Improved | -18% | 0% |
| case-10 | ✗→✓ | ▲ Improved | 41% | 0% |
| case-11 | ✗→✓ | ▲ Improved | -50% | 0% |
| case-13 | ✗→✓ | ▲ Improved | -7% | 0% |
Never use Date.now() or new Date() inside query functions. It prevents proper caching and breaks reactive subscriptions.
Queries should be deterministic. Using Date.now() means the query returns different results every millisecond, defeating Convex's reactivity system.
typescriptexport const getActiveTasks = query({ handler: async (ctx) => { const now = Date.now(); // ❌ Don't do this return await ctx.db .query("tasks") .filter(q => q.lt(q.field("dueDate"), now)) .collect(); }, });
typescriptexport const getActiveTasks = query({ args: { now: v.number() }, handler: async (ctx, args) => { return await ctx.db .query("tasks") .filter(q => q.lt(q.field("dueDate"), args.now)) .collect(); }, }); // Client passes current time const tasks = useQuery(api.tasks.getActiveTasks, { now: Date.now() });
typescript// Update status periodically with a cron job export const updateTaskStatuses = internalMutation({ handler: async (ctx) => { const now = Date.now(); const expiredTasks = await ctx.db .query("tasks") .withIndex("by_status", q => q.eq("status", "active")) .filter(q => q.lt(q.field("dueDate"), now)) .collect(); for (const task of expiredTasks) { await ctx.db.patch(task._id, { status: "expired" }); } }, }); // Query is simple and efficient export const getActiveTasks = query({ handler: async (ctx) => { return await ctx.db .query("tasks") .withIndex("by_status", q => q.eq("status", "active")) .collect(); }, });
If you need day-level filtering:
typescriptexport const getToday = query({ args: { today: v.string() }, // "2024-01-15" handler: async (ctx, args) => { return await ctx.db .query("events") .withIndex("by_date", q => q.eq("date", args.today)) .collect(); }, });
Other measured skills in the registry, with their headline benchmark lift.