迭代100万张记录时的nodejs块

发布于 2025-01-21 08:37:47 字数 1400 浏览 0 评论 0原文

/* *此功能的编写是为了使数据以大尺寸(在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文