run-in-batch
Version:
Run your task like api calls in batch
22 lines (18 loc) • 690 B
JavaScript
/**
* Chunking using generators and array splice to be efficient on memory.
* Generator makes sure that we create a chunk only when requested.
* Array splice makes sure that chuck processed gets freed from memory.
* @param {Array} arr Array of objects
* @param {Number} batchSize Batch size
*/
function* chunk(arr = [], batchSize) {
// This is a check if object, map, etc is passed
if (!Array.isArray(arr)) throw new TypeError("Pass a valid array");
// TODO change the check
if (isNaN(batchSize)) yield arr;
const batch_count = Math.ceil(arr.length / batchSize);
for (let i = 0; i < batch_count; i++) {
yield arr.splice(0, batchSize);
}
}
module.exports = chunk;