Skip to content

Instantly share code, notes, and snippets.

@romgrk
Created October 24, 2025 21:52
Show Gist options
  • Select an option

  • Save romgrk/14418d2fc53430d33ebf99715d9e2e2c to your computer and use it in GitHub Desktop.

Select an option

Save romgrk/14418d2fc53430d33ebf99715d9e2e2c to your computer and use it in GitHub Desktop.
const items = Array.from({ length: 10_000 }, (_, i) => ({ valule: i + 1 }));
const BATCH_SIZE = 500;
async function incrementalFilter(
items: any[],
predicate: (item: any) => boolean,
batchSize: number,
onResult: (result: { items: any[]; filtered: number; done: boolean }) => void,
) {
const result: any[] = [];
for (let i = 0; i < items.length; i += batchSize) {
for (let j = 0; j < batchSize && i + j < items.length; j++) {
const item = items[i + j];
if (predicate(item)) {
result.push(item);
}
}
onResult({
items: result.slice(),
filtered: Math.min(i + batchSize, items.length),
done: false,
});
await sleep(0); // Yield to the browser, let the UI update
}
onResult({
items: result,
filtered: result.length,
done: true,
});
}
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment