tryError Documentation

Performance Optimization

Advanced techniques to optimize tryError for high-performance applications

Object Pooling

Reduce garbage collection pressure and improve performance in high-throughput scenarios by reusing error objects from a pre-allocated pool.

When to Use Object Pooling
  • High-frequency error creation (1000+ errors/second)
  • Memory-sensitive environments (edge computing, IoT)
  • Real-time applications where GC pauses matter
  • Microservices handling many concurrent requests
1import { configure, trySync } from '@try-error/core';
2
3// Enable object pooling globally
4configure({
5  performance: {
6    errorCreation: {
7      objectPooling: true,
8      poolSize: 100 // Default size
9    }
10  }
11});
12
13// Errors are now allocated from the pool
14function processRequest(data: unknown) {
15  const result = trySync(() => {
16    // Your logic here
17    if (!data) throw new Error('Invalid data');
18    return processData(data);
19  });
20  
21  // Error objects are automatically returned to the pool
22  // when they go out of scope
23  return result;
24}

Lazy Evaluation

Defer expensive computations like stack trace generation until they're actually needed, significantly reducing the cost of error creation.

Performance Impact

Without Lazy Evaluation

  • • Stack trace: ~0.5-2ms per error
  • • Source location: ~0.1-0.3ms
  • • Total: ~0.6-2.3ms per error

With Lazy Evaluation

  • • Initial creation: ~0.01ms
  • • On-demand computation
  • • Zero cost if not accessed
1import { configure, trySync } from '@try-error/core';
2
3// Enable lazy evaluation globally
4configure({
5  performance: {
6    errorCreation: {
7      lazyStackTrace: true,
8      lazySourceLocation: true
9    }
10  }
11});
12
13// Stack traces are now computed on-demand
14const result = trySync(() => riskyOperation());
15
16if (isTryError(result)) {
17  // Stack trace is computed only when accessed
18  console.error(result.stack);
19  
20  // Source location is also lazy
21  console.error(`Error at ${result.source}`);
22}

Performance Configuration

Fine-tune tryError's performance characteristics for your specific use case.

1import { configure, ConfigPresets } from '@try-error/core';
2
3// Use preset configurations
4configure(ConfigPresets.minimal()); // Maximum performance
5configure(ConfigPresets.production()); // Balanced
6configure(ConfigPresets.development()); // Full debugging
7
8// Or create custom configuration
9configure({
10  performance: {
11    errorCreation: {
12      objectPooling: true,
13      poolSize: 200,
14      lazyStackTrace: true,
15      lazySourceLocation: true
16    },
17    contextCapture: {
18      maxDepth: 3,
19      maxProperties: 50,
20      excludePatterns: ['password', 'token', 'secret']
21    },
22    memoryManagement: {
23      maxErrorsInMemory: 1000,
24      errorTTL: 60000 // 1 minute
25    }
26  },
27  
28  // Minimal mode for maximum performance
29  minimalErrors: true,
30  skipTimestamp: true,
31  skipContext: true,
32  skipSourceLocation: true
33});

Benchmarks

Real-world performance measurements across different configurations.

Error Creation Performance
Operations per second (higher is better)
Native throw/catch
~500K ops/sec
tryError (minimal mode)
~450K ops/sec
tryError (with pooling)
~400K ops/sec
tryError (lazy evaluation)
~420K ops/sec
tryError (default)
~350K ops/sec
tryError (full features)
~250K ops/sec

Best Practices

Choose the Right Configuration
  • • Use ConfigPresets.minimal() for hot paths
  • • Enable object pooling for high-frequency errors
  • • Use lazy evaluation when stack traces are rarely needed
  • • Disable features you don't need (timestamps, context, etc.)
Monitor Performance
// Track error creation rate
let errorCount = 0;
setInterval(() => {
  console.log(`Errors/sec: ${errorCount}`);
  errorCount = 0;
}, 1000);

// Monitor pool efficiency
setInterval(() => {
  const stats = getErrorPoolStats();
  if (stats && stats.hitRate < 0.8) {
    console.warn('Pool hit rate low:', stats.hitRate);
  }
}, 5000);