Skip to content

Instantly share code, notes, and snippets.

@imana97
Created November 2, 2016 00:39
Show Gist options
  • Save imana97/bcee29faf63a72341430e8abf6762c9d to your computer and use it in GitHub Desktop.
Save imana97/bcee29faf63a72341430e8abf6762c9d to your computer and use it in GitHub Desktop.
Bulk Process of large arrays in Javascript
module.exports=function(array,bulkSize,job){
if (!array) throw new Error('Array is not specified');
if (!job) throw new Error('Must define the job');
var i = 0;
var j = array.length;
var chunkArr = []; // this keeps the chunk values
var chunk=bulkSize || 500000; // default value is half a million
var looper = function () {
job(array.slice(i, i + chunk),function(processReport){
i += chunk;
if (i < j) {
processReport(parseInt((i/j)*100));
looper();
} else {
processReport(100);
}
});
}; // end lo
// init looper
looper();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment