我正在尝试使用将 250000 个对象的数组导出到 excel 文件,node.js
但服务器每次都失败:
致命错误:CALL_AND_RETRY_LAST 分配失败 - JavaScript 堆内存不足
我总是用npm --max-old-space-size=8000 start
这是我尝试使用excel4node
模块生成 excel 文件的代码的一部分:
// Create a new instance of a Workbook class
var wb = new xl.Workbook();
// Add Worksheets to the workbook
var ws = wb.addWorksheet('Sheet 1');
for(const [idx, request] of report.entries()) {
let counter = 1;
for(const [_, val] of Object.keys(request)) {
ws.cell(idx + 1, counter).string(val);
counter++;
}
}
wb.write('Excel.xlsx');
这是report
数组的内容:
[
{
"Page URL": "http://www.example.com",
"Request URL": "http://use.typekit.net/yse3oeo.js",
"Domain": "typekit.net",
"OP Tag Category": "Data Management",
"OP Tag Name": "Adobe TypeKit",
"Registrar Name": "Adobe Systems Incorporated",
"Method": "GET",
"Type": "Script",
"mimeType": "text/javascript",
"Status": 200,
"Content Encoding": "gzip",
"Encoded Data Length": 8028,
"Action": null,
"IP Address": "92.123.20.219",
"Geolocation": "FR",
"Match Regex": null,
"Not Match Regex": null,
"Error": null,
"Chrome Initiator": "http://example.com",
"Final Page URL": null,
"Initial Page Status": null
}
...250000 more objects
]
我也尝试使用该mongo-xlsx
模块,但它失败并出现同样的错误......
有什么方法可以提高我的代码效率以减少内存使用量?或者也许有更好的方法来做到这一点?