Skip to content

Instantly share code, notes, and snippets.

@alexanderson1993
Created December 5, 2025 18:52
Show Gist options
  • Select an option

  • Save alexanderson1993/8c05aa2f2003bf07e11f4103652a7014 to your computer and use it in GitHub Desktop.

Select an option

Save alexanderson1993/8c05aa2f2003bf07e11f4103652a7014 to your computer and use it in GitHub Desktop.
Simple server-side TanStack DB benchmark
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