Created
December 5, 2025 18:52
-
-
Save alexanderson1993/8c05aa2f2003bf07e11f4103652a7014 to your computer and use it in GitHub Desktop.
Simple server-side TanStack DB benchmark
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
| import { | |
| createCollection, | |
| eq, | |
| liveQueryCollectionOptions, | |
| localOnlyCollectionOptions, | |
| lt, | |
| } from "@tanstack/db"; | |
| import { z } from "zod"; | |
| const testCollection = createCollection( | |
| localOnlyCollectionOptions({ | |
| id: "test", | |
| schema: z.object({ id: z.number(), value: z.number().default(0) }), | |
| getKey(item) { | |
| return item.id; | |
| }, | |
| }) | |
| ); | |
| let changes = []; | |
| for (let i = 0; i < 50; i++) { | |
| const subscription = createCollection( | |
| liveQueryCollectionOptions({ | |
| query: (q) => | |
| q | |
| .from({ test: testCollection }) | |
| .where(({ test }) => | |
| lt(test.id, Math.random() * 100 + Math.random() * 500 + 1) | |
| ) | |
| .select(({ test }) => ({ id: test.id, value: test.value })), | |
| }) | |
| ); | |
| subscription.startSyncImmediate(); | |
| subscription.subscribeChanges((c) => { | |
| for (let i of c) { | |
| changes.push(i); | |
| } | |
| }); | |
| } | |
| for (let i = 0; i < 1000; i++) { | |
| testCollection.insert({ id: i }); | |
| } | |
| setInterval(() => { | |
| const id = Math.trunc(Math.random() * 99 + 1); | |
| testCollection.update(id, (draft) => { | |
| draft.value = Math.random(); | |
| }); | |
| }, 0); | |
| let count = 0; | |
| setInterval(() => { | |
| count++; | |
| console.log(count, changes.length); | |
| count = 0; | |
| changes = []; | |
| }, 1000 / 60); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment