function isDataGreaterThanMB(data) {
// 首先将数据转换为字节大小
let dataSize = 0;
if (typeof data === 'string') {
// 如果是字符串,则获取字符串的字节长度
dataSize = new TextEncoder().encode(data).length;
} else {
// 如果是对象或数组,则将其转换为 JSON 字符串后再获取字节长度
dataSize = new TextEncoder().encode(JSON.stringify(data)).length;
}
// 将字节大小转换为 KB 或 MB
const sizeInKB = dataSize / 1024;
const sizeInMB = sizeInKB / 1024;
return sizeInMB;
}
function sizeof(obj) {
let size = 0;
if (typeof obj === 'object') {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
size += sizeof(obj[key]);
}
}
} else if (typeof obj === 'string') {
size = new TextEncoder().encode(obj).length;
} else {
size = 1; // 其他类型默认为1字节
}
return size / (1024 * 1024);
}
04-19
3038