~/workspace/source/README.md:180
Performance Characteristics
Throughput
- 20M+ API ops/sec: In-memory operations on modern hardware
- Sub-millisecond latency: For local reads and writes
- Streaming writes: No blocking on disk I/O
- Batched network I/O: Multiple messages sent together
Memory Efficiency
- 9KB core engine: Minimal browser footprint
- Configurable memory limits: Control cache size
- Automatic garbage collection: Least-recently-used eviction
~/workspace/source/src/mesh.js:15-16
Optimization Strategies
1. Minimize Subscriptions
Each.on() creates a live subscription. Limit subscriptions to only what you need:
~/workspace/source/src/mesh.js:336
2. Batch Writes
Group multiple writes into a single operation:3. Use Property Paths
Access nested data efficiently:4. Limit Graph Depth
Deeply nested graphs are slower to traverse:5. Configure Message Batching
~/workspace/source/src/mesh.js:14-17
- gap: Lower values = faster sync, higher network overhead
- pack: Larger values = more efficient network use, higher latency
- puff: Higher values = faster processing, more CPU blocking
6. Enable Super Peer Mode
For relay peers, enable super mode to optimize routing:~/workspace/source/lib/server.js:8-9
Warning: Only enable faith: true on trusted relay peers, not in browsers.
Storage Performance
In-Memory Only
For maximum performance, run without persistence:File System Storage
Node.js file system storage:RAD Storage
GUN’s optimized storage format:S3 Storage
Amazon S3 integration:Network Performance
WebSocket vs HTTP
WebSocket provides significant performance benefits:- Persistent connection: No TCP handshake overhead
- Bidirectional: Real-time updates
- Lower latency: No HTTP header overhead
Connection Pooling
Limit concurrent peer connections:~/workspace/source/lib/webrtc.js:36
Multicast for Local Networks
UDP multicast is extremely fast for local networks:~/workspace/source/lib/multicast.js:10-13
JSON Performance
Async JSON Parsing
GUN supports asynchronous JSON parsing to prevent UI blocking:~/workspace/source/src/mesh.js:5-8
Warning System
GUN warns if JSON operations take too long:~/workspace/source/src/mesh.js:8
CPU Optimization
Event Loop Scheduling
GUN usessetTimeout.turn for cooperative multitasking:
~/workspace/source/src/mesh.js:18
Configurable Processing Rate
~/workspace/source/src/mesh.js:17
Monitoring Performance
Enable Statistics
~/workspace/source/src/mesh.js:103, 195, 328
Message Counters
~/workspace/source/src/mesh.js:103, 195
Latency Tracking
Profiling
Node.js Profiling
GUN includes built-in profiling support:~/workspace/source/package.json:12
Browser Profiling
Use browser DevTools:- Open Performance tab
- Record while using your app
- Look for GUN-related function calls
- Identify bottlenecks
Benchmarking
Write Performance
Read Performance
Production Optimizations
1. Use a CDN
2. Enable Compression
3. Use Process Clustering
~/workspace/source/examples/http.js:2-5
4. Configure Timeouts
~/workspace/source/src/mesh.js:330
5. Limit Message Size
~/workspace/source/src/mesh.js:15
Large messages are rejected:
Source: ~/workspace/source/src/mesh.js:26
Common Performance Issues
Issue: High Memory Usage
Cause: Too many subscriptions or large datasets in memory Solution:Issue: Slow Initial Load
Cause: Loading large datasets from storage Solution:Issue: Network Congestion
Cause: Too many messages, large messages Solution:Issue: CPU Spikes
Cause: Large JSON parsing, synchronous operations Solution:Best Practices
- Use
.once()for one-time reads: Avoid unnecessary subscriptions - Batch writes: Group multiple updates together
- Limit graph depth: Keep data structures flat
- Configure memory limits: Prevent unbounded growth
- Enable async JSON parsing: Add
gun/lib/yson.js - Monitor statistics: Track performance metrics
- Profile regularly: Identify bottlenecks early
- Use super peer mode: Optimize relay peers
- Implement pagination: Don’t load entire datasets
- Unsubscribe when done: Clean up listeners
Performance Checklist
- Using
.once()instead of.on()where possible - Batching writes together
- Configured appropriate memory limits
- Added
gun/lib/yson.jsfor async JSON - Limited active subscriptions
- Monitoring performance metrics
- Using WebSocket for real-time sync
- Enabled compression on server
- Profiled application under load
- Optimized graph structure depth
Next Steps
- Understand CAP Theorem tradeoffs
- Learn about Conflict Resolution
- Configure Networking for your topology
- Deploy to Production