Skip to content

Instantly share code, notes, and snippets.

@HuakunShen
Created October 8, 2024 00:03
Show Gist options
  • Select an option

  • Save HuakunShen/ca739a4e22ae8cc297ffabf67e8242fe to your computer and use it in GitHub Desktop.

Select an option

Save HuakunShen/ca739a4e22ae8cc297ffabf67e8242fe to your computer and use it in GitHub Desktop.
Analyze Bun Bundle Size Distribution
const bundle = await Bun.file("dist/index.js").text()
const lines = bundle.split("\n")
// console.log(lines);
const commentLineIndices = lines
.map((line, index) => (line.trim().startsWith("//") ? index : -1))
.filter((index) => index !== -1)
console.log(commentLineIndices)
const sizeMap: Record<string, number> = {}
for (let i = 0; i < commentLineIndices.length; i++) {
const start = commentLineIndices[i]
const end = commentLineIndices[i + 1] || lines.length
const inBetweenLines = lines.slice(start, end)
const comment = inBetweenLines.join("\n")
const size = comment.length
sizeMap[lines[commentLineIndices[i]]] = size
}
// console.log(sizeMap)
// sort by size ascending
const sorted = Object.entries(sizeMap).sort((a, b) => a[1] - b[1])
console.log(sorted)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment