| 651 | * @returns Array of results |
| 652 | */ |
| 653 | export async function processInBatches<T, R>( |
| 654 | items: T[], |
| 655 | batchSize: number, |
| 656 | processor: (item: T, index: number) => Promise<R>, |
| 657 | onBatchComplete?: (completed: number, total: number) => void |
| 658 | ): Promise<R[]> { |
| 659 | const results: R[] = []; |
| 660 | |
| 661 | for (let i = 0; i < items.length; i += batchSize) { |
| 662 | const batch = items.slice(i, Math.min(i + batchSize, items.length)); |
| 663 | const batchResults = await Promise.all( |
| 664 | batch.map((item, idx) => processor(item, i + idx)) |
| 665 | ); |
| 666 | results.push(...batchResults); |
| 667 | |
| 668 | if (onBatchComplete) { |
| 669 | onBatchComplete(Math.min(i + batchSize, items.length), items.length); |
| 670 | } |
| 671 | |
| 672 | // Allow GC between batches |
| 673 | if (global.gc) { |
| 674 | global.gc(); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | return results; |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Simple mutex lock for preventing concurrent operations |