Skip to main content
GUN is designed for high-performance, real-time applications. The engine is capable of 20M+ API operations per second while maintaining a tiny footprint of just ~9KB gzipped. Source: ~/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
Source: ~/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:
GUN warns when you have more than 10,000 active subscriptions: Source: ~/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

Source: ~/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:
Source: ~/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:
Source: ~/workspace/source/lib/webrtc.js:36

Multicast for Local Networks

UDP multicast is extremely fast for local networks:
Source: ~/workspace/source/lib/multicast.js:10-13

JSON Performance

Async JSON Parsing

GUN supports asynchronous JSON parsing to prevent UI blocking:
YSON (yielding JSON) parses large payloads without blocking the event loop. Source: ~/workspace/source/src/mesh.js:5-8

Warning System

GUN warns if JSON operations take too long:
Source: ~/workspace/source/src/mesh.js:8

CPU Optimization

Event Loop Scheduling

GUN uses setTimeout.turn for cooperative multitasking:
Source: ~/workspace/source/src/mesh.js:18

Configurable Processing Rate

Source: ~/workspace/source/src/mesh.js:17

Monitoring Performance

Enable Statistics

Source: ~/workspace/source/src/mesh.js:103, 195, 328

Message Counters

Source: ~/workspace/source/src/mesh.js:103, 195

Latency Tracking

Profiling

Node.js Profiling

GUN includes built-in profiling support:
Source: ~/workspace/source/package.json:12

Browser Profiling

Use browser DevTools:
  1. Open Performance tab
  2. Record while using your app
  3. Look for GUN-related function calls
  4. Identify bottlenecks

Benchmarking

Write Performance

Read Performance

Production Optimizations

1. Use a CDN

2. Enable Compression

3. Use Process Clustering

Source: ~/workspace/source/examples/http.js:2-5

4. Configure Timeouts

Source: ~/workspace/source/src/mesh.js:330

5. Limit Message Size

Source: ~/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

  1. Use .once() for one-time reads: Avoid unnecessary subscriptions
  2. Batch writes: Group multiple updates together
  3. Limit graph depth: Keep data structures flat
  4. Configure memory limits: Prevent unbounded growth
  5. Enable async JSON parsing: Add gun/lib/yson.js
  6. Monitor statistics: Track performance metrics
  7. Profile regularly: Identify bottlenecks early
  8. Use super peer mode: Optimize relay peers
  9. Implement pagination: Don’t load entire datasets
  10. 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.js for 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