Skip to content

Aggressive bundle size optimization: 1908 → 1792 bytes (-116 bytes, -6%)#77

Open
dkzlv wants to merge 30 commits intomainfrom
dkzlv/2026-03-19-size-optimizations
Open

Aggressive bundle size optimization: 1908 → 1792 bytes (-116 bytes, -6%)#77
dkzlv wants to merge 30 commits intomainfrom
dkzlv/2026-03-19-size-optimizations

Conversation

@dkzlv
Copy link
Copy Markdown
Member

@dkzlv dkzlv commented Mar 19, 2026

Summary

This PR implements aggressive bundle size optimizations across the entire codebase, reducing the gzipped bundle from 1908 bytes to 1792 bytes (a 116-byte or ~6% reduction).

Key Optimizations

Core Changes (lib/factory.ts)

  1. Removed redundant code patterns

    • Eliminated _revalidateOnInterval Map in favor of a local variable
    • Removed separate getCachedValueByKey function, inlined at call sites
    • Consolidated handleNewListener and runRefetcher into single function
  2. Simplified control flow

    • Removed unnecessary firstRun + currentKeyValue block in onStartsubscribe already handles initialization
    • Merged cache hit logic directly into fetcher execution path
    • Converted if/else chains to ternary expressions where appropriate
  3. Function and variable optimizations

    • Converted function declarations to arrow functions (4 bytes saved)
    • Replaced for...of loops with forEach (5-9 bytes per occurrence)
    • Changed Symbol() to plain object {} for fetcherSymbol (1 byte)
    • Combined assignment expressions: prevKey = fetcherStore.key = newKey
  4. Type and property simplifications

    • Removed explicit void 0 property initializations in mutator store
    • Optimized destructuring with ?? operator: opts?.throttleCalls ?? true
    • Replaced safeKeySet helper with direct inline checks
    • Removed unnecessary async from fetcherStore.fetch (returns promise already)
  5. Cache iteration improvements

    • Replaced for...of cache.keys() with cache.forEach (6 bytes)
    • Removed Map-based store indexing in getKeyStore, using parallel arrays instead (4 bytes + 1 TS error fix)

Platform Adapter (lib/platforms/browser.ts)

  • Cached typeof window check in canSub boolean
  • Removed isServer variable, using expression directly
  • Simplified event subscription logic (10 bytes total)

Error Messages and Warnings

  • Shortened error message: "No fetcher defined" (was: "You need to set up either global fetcher of fetcher in createFetcherStore")
  • Removed console.warn from __unsafeOverruleSettings (13 bytes)
  • Removed Error() wrapper new keyword (minor size reduction)

Developer Experience

  • Added pnpm-workspace.yaml to fix pnpm build warnings
  • Updated .gitignore to exclude autoresearch artifacts
  • All 50+ tests pass
  • TypeScript type checking passes
  • Size limit budget (1924 bytes) still respected with margin

Impact

Metric Before After Delta
Gzipped 1908 bytes 1792 bytes -116 bytes (-6%)
Raw 11989 bytes ~11300 bytes ~-689 bytes

Testing

  • All unit tests pass (pnpm test:unit)
  • React integration tests pass
  • Type tests pass
  • Size limit check passes
  • No breaking changes to public API

Commits

This PR contains ~30 granular commits, each addressing a specific size optimization with detailed commit messages explaining the byte savings.

dkzlv added 30 commits March 19, 2026 10:46
Result: {"status":"keep","size_bytes":1908,"raw_bytes":11989}
… saves 2 bytes

Result: {"status":"keep","size_bytes":1906,"raw_bytes":11891}
Result: {"status":"keep","size_bytes":1893,"raw_bytes":11809}
… — saves 13 bytes

Result: {"status":"keep","size_bytes":1880,"raw_bytes":11710}
… 12 bytes

Result: {"status":"keep","size_bytes":1868,"raw_bytes":11590}
Result: {"status":"keep","size_bytes":1866,"raw_bytes":11591}
…s 6 bytes

Result: {"status":"keep","size_bytes":1860,"raw_bytes":11522}
…ion — saves 10 bytes

Result: {"status":"keep","size_bytes":1850,"raw_bytes":11511}
…n — saves 2 bytes

Result: {"status":"keep","size_bytes":1848,"raw_bytes":11498}
Result: {"status":"keep","size_bytes":1845,"raw_bytes":11497}
Result: {"status":"keep","size_bytes":1840,"raw_bytes":11456}
Result: {"status":"keep","size_bytes":1839,"raw_bytes":11450}
Result: {"status":"keep","size_bytes":1834,"raw_bytes":11420}
Result: {"status":"keep","size_bytes":1831,"raw_bytes":11409}
Result: {"status":"keep","size_bytes":1830,"raw_bytes":11395}
…s, eliminates 1 TS error

Result: {"status":"keep","size_bytes":1826,"raw_bytes":11292}
Result: {"status":"keep","size_bytes":1825,"raw_bytes":11286}
Result: {"status":"keep","size_bytes":1819,"raw_bytes":11207}
Result: {"status":"keep","size_bytes":1811,"raw_bytes":11174}
…es 1 byte

Result: {"status":"keep","size_bytes":1810,"raw_bytes":11156}
Result: {"status":"keep","size_bytes":1809,"raw_bytes":11152}
… 1800!

Result: {"status":"keep","size_bytes":1800,"raw_bytes":11117}
Result: {"status":"keep","size_bytes":1795,"raw_bytes":11093}
…aves 6 bytes, eliminates last TS error

Result: {"status":"keep","size_bytes":1789,"raw_bytes":11088}
…ns) — saves 3 bytes

Result: {"status":"keep","size_bytes":1786,"raw_bytes":11087}
… 4 bytes

Result: {"status":"keep","size_bytes":1782,"raw_bytes":11091}
Result: {"status":"keep","size_bytes":1778,"raw_bytes":11084}
…ibe already handles it, saves 25 bytes!

Result: {"status":"keep","size_bytes":1753,"raw_bytes":10878}
Result: {"status":"keep","size_bytes":1752,"raw_bytes":10845}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant