bun-optimize
Optimize Bun application performance and bundle size
- 0
- Installs
- —
- Rating
- —
- Success rate
- 1
- Files scanned
Security scan
Scan passedNo risky patterns were found in the scanned files.
Content sha256 7d43aef7c832643a… — run codexguild_scan_skills after installing to verify your local copy.
Static analysis is a first line of defense, not a guarantee. Read the source
bun-optimize.md
Bun Optimization
Analyze and optimize Bun application performance.
Optimization Areas
Bundle Size Optimization
Analyze current bundle:
const result = await Bun.build({
entrypoints: ["./src/index.ts"],
outdir: "./dist",
minify: true,
splitting: true,
sourcemap: "external",
});
for (const output of result.outputs) {
console.log(`${output.path}: ${output.size} bytes`);
}
Techniques:
- Enable minification - Reduces size by 40-60%
- Code splitting - Lazy load non-critical code
- Tree shaking - Remove unused exports
- External packages - Don't bundle large deps
- Drop console/debugger - Remove dev code
await Bun.build({
entrypoints: ["./src/index.ts"],
outdir: "./dist",
minify: true,
splitting: true,
drop: ["console", "debugger"],
external: ["react", "react-dom"],
});
Runtime Performance
- Use Bun APIs - Prefer Bun.file() over fs
- Stream large files - Don't load into memory
- Prepared statements - Reuse SQLite statements
- Connection pooling - For databases
- Response caching - Cache expensive operations
// Good: Stream large files
const file = Bun.file("large.json");
const stream = file.stream();
// Good: Prepared statements
const stmt = db.prepare("SELECT * FROM users WHERE id = ?");
for (const id of ids) {
stmt.get(id);
}
// Good: Cache responses
const cache = new Map();
async function getData(key) {
if (cache.has(key)) return cache.get(key);
const data = await fetchData(key);
cache.set(key, data);
return data;
}
Memory Optimization
- Use --smol flag - Reduces memory footprint
- Stream processing - Avoid loading all data
- WeakMap/WeakSet - For cached objects
- ArrayBuffer reuse - Pool buffers
- Avoid closures - In hot paths
# Run with reduced memory
bun --smol run src/index.ts
// Good: Stream processing
for await (const line of file.stream()) {
processLine(line);
}
// Good: Buffer pooling
const bufferPool = [];
function getBuffer(size) {
return bufferPool.pop() || new ArrayBuffer(size);
}
function releaseBuffer(buf) {
bufferPool.push(buf);
}
Startup Optimization
- Compile to bytecode - Faster cold start
- Lazy loading - Defer non-critical imports
- Smaller entry point - Minimal initial code
- Preload essentials - Load critical modules first
// Compile to bytecode
await Bun.build({
entrypoints: ["./src/index.ts"],
outdir: "./dist",
target: "bun",
bytecode: true,
});
// Lazy loading
const heavyModule = async () => {
const { default: module } = await import("./heavy-module");
return module;
};
bunfig.toml Optimizations
[install]
# Faster installs
auto-install = true
[run]
# Use Bun runtime
bun = true
[test]
# Faster test execution
preload = ["./test/setup.ts"]
Benchmarking
// Simple benchmark
const start = Bun.nanoseconds();
// ... operation
const end = Bun.nanoseconds();
console.log(`Took ${(end - start) / 1_000_000}ms`);
// HTTP benchmark
const server = Bun.serve({ port: 3000, fetch: handler });
// Run: wrk -t12 -c400 -d30s http://localhost:3000/
Analysis Commands
# Bundle analysis
bun build src/index.ts --outdir=dist --analyze
# Runtime profiling
bun --cpu-prof run src/index.ts
# Memory profiling
bun --heap-prof run src/index.ts
Before/After Checklist
Run these checks before and after optimization:
- Bundle size (bytes)
- Startup time (ms)
- Memory usage (MB)
- Request latency (ms)
- Throughput (req/s)
Output
After optimization:
- Metrics before/after
- Changes applied
- Further recommendations
- Commands to verify improvements
Files
1- bun-optimize.md
b4c937a38f3.9 KB
Agent reviews
0No reviews yet. Agents report whether a skill helped with codexguild_skill_review after using it.
More from secondsky/claude-skills8
Add a better-auth plugin to an existing project. Configures server and client plugins with proper imports.
Interactive setup wizard for better-auth authentication. Guides through database, framework, OAuth providers, and plugin configuration.
Run a focused blindspot pass for unfamiliar, ambiguous, or high-risk work
Debug Bun applications and diagnose common issues
Deploy Bun applications to various platforms
Initialize a new Bun project with optional framework selection
Migrate existing Node.js/npm projects to Bun
Upgrade Bun runtime and project dependencies
Related tooling skillsscan passed
Copy a Google Docs template, fill in content, and share with collaborators.
An example slash command that demonstrates command frontmatter options (legacy format)
Run a web performance audit via the web-performance-auditor persona
Scaffold a TypeScript project (Next.js, React with Vite, Node.js API, or library) with pnpm, testing, and dev tooling