Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
ByteByte
(version: 0)
bytymighty
Comparing performance of:
f1 vs f2 vs f3 vs f4 vs f5
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function format1(bytes) { var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; if (bytes == 0) return '0 Byte'; var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i]; } function format2(bytes, decimals = 2) { if (bytes === 0) return '0 Bytes'; const k = 1024; const dm = decimals < 0 ? 0 : decimals; const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; } function format3(bytes) { var marker = 1024; // Change to 1000 if required var decimal = 3; // Change as required var kiloBytes = marker; // One Kilobyte is 1024 bytes var megaBytes = marker * marker; // One MB is 1024 KB var gigaBytes = marker * marker * marker; // One GB is 1024 MB var teraBytes = marker * marker * marker * marker; // One TB is 1024 GB // return bytes if less than a KB if(bytes < kiloBytes) return bytes + " Bytes"; // return KB if less than a MB else if(bytes < megaBytes) return(bytes / kiloBytes).toFixed(decimal) + " KB"; // return MB if less than a GB else if(bytes < gigaBytes) return(bytes / megaBytes).toFixed(decimal) + " MB"; // return GB if less than a TB else return(bytes / gigaBytes).toFixed(decimal) + " GB"; } const units = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; function format4(x){ let l = 0, n = parseInt(x, 10) || 0; while(n >= 1024 && ++l){ n = n/1024; } return(n.toFixed(n < 10 && l > 0 ? 1 : 0) + ' ' + units[l]); } function format5(bytes){ var kb = 1024; var ndx = Math.floor( Math.log(bytes) / Math.log(kb) ); var fileSizeTypes = ["bytes", "kb", "mb", "gb", "tb", "pb", "eb", "zb", "yb"]; return { size: +(bytes / kb / kb).toFixed(2), type: fileSizeTypes[ndx] }; }
Tests:
f1
for(var i =0; i<100; i++){ var a = format1(i*1024); }
f2
for(var i =0; i<100; i++){ var a = format2(i*1024); }
f3
for(var i =0; i<100; i++){ var a = format3(i*1024); }
f4
for(var i =0; i<100; i++){ var a = format4(i*1024); }
f5
for(var i =0; i<100; i++){ var a = format5(i*1024); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
f1
f2
f3
f4
f5
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared options, pros and cons, library usage, special JavaScript features, and other considerations. **Benchmark Overview** The benchmark measures the performance of five different functions for formatting byte sizes: `format1`, `format2`, `format3`, `format4`, and `format5`. Each function takes an integer value as input and returns a string representation of the corresponding byte size (e.g., "1024 Bytes"). **Comparison Options** The five functions being compared are: 1. `format1`: uses logarithms to calculate the number of bytes, then rounds the result to 2 decimal places. 2. `format2`: uses logarithms with base 1024, similar to `format1`, but with optional decimal precision. 3. `format3`: uses a manual approach with fixed values for kilobytes, megabytes, gigabytes, and terabytes. 4. `format4`: uses a simple iterative approach with 10 divisions by 1024. 5. `format5`: returns an object with the size value and unit (e.g., "size": 1024, "type": "Bytes"). **Pros and Cons** Here are some pros and cons for each function: 1. `format1`: * Pros: simple and efficient using logarithms. * Cons: may lose precision due to rounding. 2. `format2`: * Pros: flexible decimal precision, similar to `format1`. * Cons: slightly slower due to additional calculations. 3. `format3`: * Pros: easy to understand and maintain, with fixed values. * Cons: less efficient than logarithmic approaches. 4. `format4`: * Pros: simple and fast, but may not be accurate for large values. * Cons: prone to rounding errors due to repeated divisions. 5. `format5`: * Pros: returns an object with useful information, easy to parse. * Cons: slower than the other functions due to object creation. **Library Usage** None of the functions use any external libraries or built-in JavaScript methods beyond basic arithmetic operations. **Special JavaScript Features** The benchmark does not utilize any special JavaScript features beyond standard arithmetic and string manipulation. However, it's worth noting that some browsers may implement optimizations for specific functions (e.g., `format2` with decimal precision). **Other Considerations** * The benchmark measures the number of executions per second, which implies a focus on performance. * The test is run in a desktop environment, which might not be representative of mobile or web-based scenarios. * The results are provided for Firefox 90 on Mac OS X 10.15, with varying execution counts and times. Overall, this benchmark provides a comparison of five different approaches to formatting byte sizes, highlighting the trade-offs between simplicity, efficiency, accuracy, and flexibility.
Related benchmarks:
Decimal rounding
Intl.NumberFormat vs toLocalString vs string split vs toFixed vs bignumber
Size format functions - fix
Decimal Rounding - Exponential notation vs Number.EPSILON vs Double rounding vs Double rounding v2
Comments
Confirm delete:
Do you really want to delete benchmark?