Agent skill
optimize-js-library-size
Optimize JavaScript library size
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/optimize-js-library-size
SKILL.md
Reduce the size of this JavaScript library.
Goals
- Reduce the minified and gzipped/brotli minified size
- NEVER reduce minified size at the expense of gzipped/brotli minified size
- ALWAYS preserve the public API surface
- Focus on large structural changes instead of small wins
Workflow
- Read and understand the library structure
- Run relevant tests and note which ones already fail (do NOT fix them)
- Measure size
- Optimize
- Rebuild
- Measure size and compare: if size didn't improve, then revert and go back to step 4
- Run relevant tests: if tests are newly failing, then revert and go back to step 4
- Repeat from step 4
Tools
-
Ensure
terseris configured:js{ // Assume modern JavaScript ecma: 2020, module: true, toplevel: true, // Run multiple times compress: { passes: 3, }, // Mangle underscore prefixed properties mangle: { properties: { regex: `^_[^_]+`, }, }, } -
Use
nodeto measure size:bashnode --input-type=module << 'EOF' import { readFileSync } from 'fs' import { gzipSync, brotliCompressSync } from 'zlib' const raw = readFileSync('REPLACE_ME.js', 'utf8') const minified = Buffer.from(raw.replace(/^\/\/# sourceMappingURL=[^\n]+$/m, '').trimEnd()) console.log(`Minified: ${minified.length} | Gzipped: ${gzipSync(minified).length} | Brotli: ${brotliCompressSync(minified).length}`) EOF
Techniques
High-level
- Delete dead code
- Unify repeated or similar logic
- Simplify complex logic without changing behavior
- Extract repeated code to variables or functions
Low-level
- Mangle internal objects using private class fields or underscore prefixed properties
- Replace internal classes with closures returning objects
- Inline single-use functions that don't add clarity
- Restructure single-use functions that add clarity to be inlinable by
terser - Replace
functions with arrow functions - Replace
switchwithifandelse - Replace
===/!==with==/!=when it doesn't change behavior - Replace strict checks with truthy/falsy checks when it doesn't change behavior
These aren't exhaustive. Reason from first principles when none fits cleanly.
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?