优化Again

This commit is contained in:
CN-JS-HuiBai
2026-04-04 19:56:03 +08:00
parent 694f5b5031
commit 9b113b6986
2 changed files with 43 additions and 11 deletions

View File

@@ -9,7 +9,8 @@ function formatBytes(bytes, decimals = 2) {
if (!bytes || bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
const i = Math.floor(Math.log(Math.abs(bytes)) / Math.log(k));
let i = Math.floor(Math.log(Math.abs(bytes)) / Math.log(k));
i = Math.max(0, Math.min(i, sizes.length - 1));
const value = bytes / Math.pow(k, i);
return value.toFixed(decimals) + ' ' + sizes[i];
}
@@ -21,7 +22,8 @@ function formatBandwidth(bytesPerSec, decimals = 2) {
if (!bytesPerSec || bytesPerSec === 0) return '0 B/s';
const k = 1024;
const sizes = ['B/s', 'KB/s', 'MB/s', 'GB/s', 'TB/s'];
const i = Math.floor(Math.log(Math.abs(bytesPerSec)) / Math.log(k));
let i = Math.floor(Math.log(Math.abs(bytesPerSec)) / Math.log(k));
i = Math.max(0, Math.min(i, sizes.length - 1));
const value = bytesPerSec / Math.pow(k, i);
return value.toFixed(decimals) + ' ' + sizes[i];
}