Created
November 2, 2016 00:39
-
-
Save imana97/bcee29faf63a72341430e8abf6762c9d to your computer and use it in GitHub Desktop.
Bulk Process of large arrays in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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