通过 getStorageInfoSync() 获取的 currentSize 和我们通过其keys调用 getStorageSync() 方法计算出的值相差太多,前者为10393kb,后者为四十多kb。
相关代码如下示:
const sizeof = function(str, charset){
str = typeof str === 'string' ? str : JSON.stringify(str);
let total = 0;
charset = charset ? charset.toLowerCase() : '';
if(charset === 'utf-16' || charset === 'utf16'){
for(let i = 0, len = str.length; i < len; i++){
const charCode = str.charCodeAt(i);
if(charCode <= 0xffff){
total += 2;
}else{
total += 4;
}
}
}else{
for(let i = 0, len = str.length; i < len; i++){
const charCode = str.charCodeAt(i);
if(charCode <= 0x007f) {
total += 1;
}else if(charCode <= 0x07ff){
total += 2;
}else if(charCode <= 0xffff){
total += 3;
}else{
total += 4;
}
}
}
return total;
}
const storageInfo = wx.getStorageInfoSync();
const {currentSize, limitSize, keys = []} = storageInfo;
keys.forEach((key, index) => {
setTimeout(() => {
const value = wx.getStorageSync(key);
const size = sizeof(value);
}, index * 20);
});
你好,麻烦提供能复现问题的代码片段(https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html)
这个问题只在安卓机型上出现,iOS没有这个问题