@super-zuo/zuo-batch
v0.0.1
Published
Million-level batch processing. by super-zuo
Maintainers
Readme
📦 安装
npm install @super-zuo/zuo-batch
# 或
yarn add @super-zuo/zuo-batch
import { batch } from '@super-zuo/zuo-batch';
// Simulate an async processor (e.g., DB insert, API call)
const uploadBatch = async (items: string[]) => {
console.log(`Uploading ${items.length} items...`);
await new Promise(r => setTimeout(r, 100)); // mock network delay
return items.map(item => ({ id: item, status: 'ok' }));
};
// Generate 1M mock IDs
const millionItems = Array.from({ length: 1_000_000 }, (_, i) => `item_${i}`);
// ✅ Smart batching: 10k per batch, max 10 concurrent, 50ms delay between batches
const results = await batch(millionItems, uploadBatch, {
batchSize: 10_000,
concurrency: 10,
delay: 50,
onProgress: (processed, total) => {
const percent = ((processed / total) * 100).toFixed(1);
console.log(`✅ ${processed}/${total} (${percent}%)`);
}
});
console.log('✅ All done!', results.length); // 1,000,000