最近我正在研究PouchDB,因为我们的项目可能会使用它,我做了一些调查,发现在保存很多附件时,需要花很多时间才能完成。
现在我有一个要求:将 30M大小 png文件保存为文档的附件。我尝试了两种方法来解决它,但它们都不完美,我们怎样才能提高我的功能性能呢?
1.同时保存这些文件,大约需要71237ms:
function saveImage(imgSource30M2) {
var blob = base64toBlob(imgSource30M2, 'image/png', 1024);
imgSoucrce30m = blob;
var t1 = new Date();
db.put({
_id: 'my0112doc',
_attachments: {
'myattachment.png': {
content_type: 'image/png',
data: blob
},
'myattachment2.png': {
content_type: 'image/png',
data: blob
},
'myattachment4.png': {
content_type: 'image/png',
data: blob
},
'myattachment5.png': {
content_type: 'image/png',
data: blob
},
'myattachment3.png': {
content_type: 'image/png',
data: blob
}
}
}, (err, doc) => {
var t2 = new Date();
console.log("save in pouchdb timeoff:", t2.getTime() - t1.getTime());
});
}
2.逐个保存这些文件,这样需要花费更多时间,大约需要226221ms。
const t11=new Date();
function addNewDoc(imgSoucrce30m) {
var blob = imgSoucrce30m;
addAttachment(5, blob, t11);
}
function addAttachment(counter, blob, t1) {
var nameImg = counter + '.png';
db.get('my0112doc', function(err, doc) {
if (err) {
return console.log(err);
}
db.putAttachment('my0112doc', nameImg,doc._rev ,blob, 'text/plain', function(err, res) {
if (err) {
return console.log(err);
}
if (counter >= 0) {
addAttachment(counter - 1, blob);
}
if(counter < 0 ){
var t2=new Date();
console.log("for loop save in pouchdb timeoff:", t2.getTime() - t11.getTime());
}
});
})
}
答案 0 :(得分:1)
base64ToBlob()
可能对这些庞大的数据进行非常昂贵的操作。如果您可以直接使用Blob(例如,通过<input type="file">
中的File对象,那么您应该否则,很难说你应该做什么,因为它可能只是将100个MB的Blob写入IndexedDB将会非常慢,并且除了向用户显示之外你没有其他解决方案加载微调器(在这种情况下务必使用Web Worker,使其不block the DOM。)