-
Notifications
You must be signed in to change notification settings - Fork 13k
feat: formatting for Memory used
improvement
#62375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: formatting for Memory used
improvement
#62375
Conversation
661bb51
to
91f725c
Compare
91f725c
to
29bb880
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
formatMemory
function - 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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"; | |
} | |
Copilot uses AI. Check for mistakes.
Closes #62374