Created
October 24, 2025 21:52
-
-
Save romgrk/14418d2fc53430d33ebf99715d9e2e2c to your computer and use it in GitHub Desktop.
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
| 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