TypeScript
Performance
Performance tuning
Caching
Use @Cache decorator:
@Tool({ name: 'get_config' })
@Cache({ ttl: 3600 }) // 1 hour
async getConfig() {
return await this.loadConfig();
}Rate Limiting
Prevent abuse:
@Tool({ name: 'expensive_operation' })
@RateLimit({ requests: 10, window: '1m' })
async expensiveOperation() {
// Limited to 10 requests per minute
}Database Optimization
Most slow tools are slow because of the database.
Use Indexes
Index the columns your tools filter and sort by.
CREATE INDEX idx_products_category ON products(category);
CREATE INDEX idx_users_email ON users(email);Connection Pooling
Create one connection pool in a singleton service and reuse it for every call.
@Injectable()
export class DatabaseService {
private pool = createPool({ max: 10 });
}Query Optimization
Select only the columns you need, and let the database do the filtering.
// Good - Specific query
SELECT id, name, price FROM products WHERE category = ?
// Avoid - SELECT *
SELECT * FROM productsAsync Operations
Use Promise.all for parallel operations:
// Good - Parallel
const [user, orders, preferences] = await Promise.all([
this.userService.findById(id),
this.orderService.findByUser(id),
this.preferenceService.findByUser(id)
]);
// Avoid - Sequential
const user = await this.userService.findById(id);
const orders = await this.orderService.findByUser(id);
const preferences = await this.preferenceService.findByUser(id);