feat: formatting for Memory used improvement#62375
feat: formatting for Memory used improvement#62375hamirmahal wants to merge 1 commit intomicrosoft:mainfrom hamirmahal:feat/formatting-for-memory-usage-improvement
Memory used improvement#62375Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR improves the formatting of memory usage statistics in the TypeScript compiler by replacing simple KB formatting with a more human-friendly format that automatically selects appropriate units (KB, MB, GB) based on the memory size.
- Extracts memory formatting logic into a dedicated
formatMemoryfunction - Implements dynamic unit selection (KB/MB/GB) with appropriate decimal precision
- Uses binary units (1024-based) for more accurate memory representation
| function formatMemory(bytes: number) { | ||
| // bytes -> choose KB/MB/GB with a human friendly format | ||
| const KB = 1024; | ||
| const MB = KB * 1024; | ||
| const GB = MB * 1024; | ||
| const numberOfDecimalPlaces = 1; | ||
|
|
||
| if (bytes >= GB) { | ||
| return (bytes / GB).toFixed(numberOfDecimalPlaces) + " GB"; | ||
| } | ||
|
|
||
| if (bytes >= MB) { | ||
| return (bytes / MB).toFixed(numberOfDecimalPlaces) + " MB"; | ||
| } | ||
|
|
||
| const kilobytesUsed = Math.round(bytes / KB); | ||
| return kilobytesUsed + " KB"; | ||
| } | ||
|
|
There was a problem hiding this comment.
The function handles negative input values incorrectly. Negative bytes would be compared against positive thresholds and fall through to the KB case, potentially producing misleading output like '-5 KB'. Consider adding input validation or handling negative values explicitly.
| function formatMemory(bytes: number) { | |
| // bytes -> choose KB/MB/GB with a human friendly format | |
| const KB = 1024; | |
| const MB = KB * 1024; | |
| const GB = MB * 1024; | |
| const numberOfDecimalPlaces = 1; | |
| if (bytes >= GB) { | |
| return (bytes / GB).toFixed(numberOfDecimalPlaces) + " GB"; | |
| } | |
| if (bytes >= MB) { | |
| return (bytes / MB).toFixed(numberOfDecimalPlaces) + " MB"; | |
| } | |
| const kilobytesUsed = Math.round(bytes / KB); | |
| return kilobytesUsed + " KB"; | |
| } | |
| function formatMemory(bytes: number) { | |
| // bytes -> choose KB/MB/GB with a human friendly format | |
| if (bytes < 0) { | |
| return "N/A"; | |
| } | |
| const KB = 1024; | |
| const MB = KB * 1024; | |
| const GB = MB * 1024; | |
| const numberOfDecimalPlaces = 1; | |
| if (bytes >= GB) { | |
| return (bytes / GB).toFixed(numberOfDecimalPlaces) + " GB"; | |
| } | |
| if (bytes >= MB) { | |
| return (bytes / MB).toFixed(numberOfDecimalPlaces) + " MB"; | |
| } | |
| const kilobytesUsed = Math.round(bytes / KB); | |
| return kilobytesUsed + " KB"; | |
| } | |
|
The linked issue is closed as not planned; we aren't going to take this. |
|
The TypeScript team hasn't accepted the linked issue #62374. If you can get it accepted, this PR will have a better chance of being reviewed. |
Closes #62374