Skip to main content

Bundle Size

Grain is designed to be lightweight:
  • Core SDK: ~6 KB gzipped
  • With React Hooks: ~8 KB gzipped
  • Zero dependencies: No bloat
The SDK is tree-shakeable - only bundle what you use.

Batching Strategy

Events are automatically batched for efficiency:
Higher batch size = Fewer requests, longer delays Lower batch size = More requests, shorter delays

For High-Traffic Apps

Increase batch size to reduce request volume:

For Real-Time Apps

Decrease batch size for faster delivery:

For Serverless

Disable batching, flush manually:

Configuration Caching

Remote config uses cache-first strategy for instant loading: How it works:
  1. Return cached/default value immediately (0ms)
  2. Fetch fresh value in background
  3. Update cache when received

Refresh Interval

Control how often configs refresh:
Less frequent = Fewer requests, slightly stale data More frequent = More requests, fresher data

Disable Caching

For testing or specific scenarios:
Warning: Disabling cache means waiting for network on every access.

React Hooks Optimization

Hooks are optimized to prevent unnecessary re-renders:
useAllConfigs re-renders on any config change:
Tip: Use useConfig for specific values to minimize re-renders.

Track Function Memoization

useTrack returns a stable function reference:
No need to memoize track - already optimized.

Preloading Configurations

Preload configs at app startup for zero-delay access:
Trade-off: Upfront loading time for instant access later.

Lazy Loading

Initialize Grain lazily if not needed immediately:

Network Optimization

Retry Configuration

Balance reliability with performance:
More retries = Better reliability, slower failure detection Fewer retries = Faster failure, may lose events

Beacon API

For page exit events, Beacon API ensures delivery:

Memory Management

Events are queued in memory until sent. Large queues use more memory:

Monitoring Performance

Enable debug mode to monitor performance:
Check console for:
  • Batch send times
  • Queue sizes
  • Network requests
  • Retry attempts

Code Splitting

For React apps, split Grain from main bundle:
Trade-off: Smaller initial bundle, slight delay before tracking starts.

Best Practices

1. Use Default Settings: Optimized for most cases 2. Profile Before Optimizing: Measure actual impact 3. Batch Wisely: Balance freshness with efficiency 4. Cache Aggressively: Configs should cache by default 5. Lazy Load When Possible: Don’t block critical path

Measuring Impact

Track Grain’s performance impact:
Track operations are nearly instant (just queue), network happens in background.

Next Steps

Configuration

Tune configuration options

Error Handling

Handle failures gracefully