迭代100万张记录时的nodejs块
/* *此功能的编写是为了使数据以大尺寸(在MB IE约143 MB中)作为输入,并以30 MB的形式进行块并执行下一个操作... *使用'object-sizeof'npm模块来计算每个迭代的尺寸 */
const Q = require('q');
const sizeof = require('object-sizeof');
function insertProcess(formattedData){
let deferred = Q.defer();
let finalData = [];
// here data limit is 30 mb i.e. out of 143 mb want to send data 30 mb at time
let dataLimit = 30;
if ((sizeof(formattedData) / 1000000) <= dataLimit) {
// some function invoked
deferred.resolve();
}
else {
async.forEachLimit(formattedData, 1000, (element, callback) => {
finalData.push(element);
if ((sizeof(finalData) / 1000000) >= dataLimit) {
// passing finaldata to another function
// removing all element from finalData by setting its length to 0
finalData.length = 0;
callback();
}
}, (error) => {
if (finalData.length) {
// passing finaldata to another function
// removing all element from finalData by setting its length to 0
finalData.length = 0;
deferred.resolve();
}
})
}
return deferred.promise;
}
/*
* This function is written to get data in big size (in Mb i.e. around 143 MB) as input and chunk it in 30 MB and perform the next operation...
* using 'object-sizeof' npm module to calculate size on every iteration
*/
const Q = require('q');
const sizeof = require('object-sizeof');
function insertProcess(formattedData){
let deferred = Q.defer();
let finalData = [];
// here data limit is 30 mb i.e. out of 143 mb want to send data 30 mb at time
let dataLimit = 30;
if ((sizeof(formattedData) / 1000000) <= dataLimit) {
// some function invoked
deferred.resolve();
}
else {
async.forEachLimit(formattedData, 1000, (element, callback) => {
finalData.push(element);
if ((sizeof(finalData) / 1000000) >= dataLimit) {
// passing finaldata to another function
// removing all element from finalData by setting its length to 0
finalData.length = 0;
callback();
}
}, (error) => {
if (finalData.length) {
// passing finaldata to another function
// removing all element from finalData by setting its length to 0
finalData.length = 0;
deferred.resolve();
}
})
}
return deferred.promise;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论