Skip to content

Performance Optimization

Optimize your Ignis application for speed and scalability.

1. Measure Performance

Identify bottlenecks before optimizing:

typescript
import { executeWithPerformanceMeasure } from '@venizia/ignis';

await executeWithPerformanceMeasure({
  logger: this.logger,
  scope: 'DataProcessing',
  description: 'Process large dataset',
  task: async () => {
    await processLargeDataset();
  },
});

Logs execution time automatically.

Deep Dive: See Performance Utility for advanced profiling.

2. Offload CPU-Intensive Tasks

Prevent blocking the event loop with Worker Threads:

Use Worker Threads for:

  • Complex calculations or crypto operations
  • Large file/data processing
  • Any synchronous task > 5ms

Deep Dive: See Worker Thread Helper for implementation guide.

3. Optimize Database Queries

TechniqueExampleImpact
Select specific fieldsfields: { id: true, name: true }Reduce data transfer
Use indexesCreate indexes on WHERE/JOIN columns10-100x faster queries
Paginate resultslimit: 20, offset: 0Prevent memory overflow
Eager load relationsinclude: [{ relation: 'creator' }]Solve N+1 problem

Example:

typescript
await userRepository.find({
  filter: {
    fields: { id: true, name: true, email: true },  // ✅ Specific fields
    where: { status: 'ACTIVE' },
    limit: 20,                                       // ✅ Pagination
    include: [{ relation: 'profile' }],             // ✅ Eager load
  },
});

4. Implement Caching

Reduce database load with caching:

Cache TypeUse CaseImplementation
RedisDistributed cache, session storageRedis Helper
In-MemorySingle-process cacheMemoryStorageHelper

Example:

typescript
// Cache expensive query results
const cached = await redis.get('users:active');
if (!cached) {
  const users = await userRepository.find({ where: { active: true } });
  await redis.set('users:active', users, 300); // 5 min TTL
}

5. Production Settings

SettingValueWhy
NODE_ENVproductionEnables library optimizations
Process ManagerPM2, systemd, DockerAuto-restart, cluster mode
Cluster ModeCPU coresUtilize all CPUs

PM2 Cluster Mode:

bash
pm2 start dist/index.js -i max  # Use all CPU cores