Created
October 8, 2024 00:03
-
-
Save HuakunShen/ca739a4e22ae8cc297ffabf67e8242fe to your computer and use it in GitHub Desktop.
Analyze Bun Bundle Size Distribution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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