Rate limiting
Limit concurrency with batchSize
Section titled “Limit concurrency with batchSize”batchSize is the number of tasks that run at the same time. The next batch starts only after every task in the current batch has settled.
// At most 5 concurrent API callsawait batchProcess(userIds, async (userId) => { return await fetchUserDataFromAPI(userId)}, { batchSize: 5 })The same option is useful for keeping memory and CPU usage under control:
// Process 10 items at a time to avoid memory issuesawait batchProcess(largeDataset, async (data) => { return await processHeavyOperation(data)}, { batchSize: 10 })Pace requests with delayBetweenBatches
Section titled “Pace requests with delayBetweenBatches”delayBetweenBatches waits the given number of milliseconds before each batch except the first one.
// 5 requests at a time, 1 second pause between batchesawait batchProcess( apiRequests, async (request) => await fetch(request.url), { batchSize: 5, delayBetweenBatches: 1000 })Splitting arrays with arrayBatch
Section titled “Splitting arrays with arrayBatch”If you only need to split an array, use arrayBatch directly:
import { arrayBatch } from 'chunk-process'
arrayBatch([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3)// [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]arrayBatch throws if batchSize is not a positive integer.