sb-dev/openquant / .agents/skills/node
node
VerifiedProvides domain-specific best practices for Node.js development with TypeScript, covering type stripping, async patterns, error handling, streams, modules, testing, performance, caching, logging, and more. Use when setting up Node.js projects with native TypeScript support, configuring type stripping (--experimental-strip-types), writing Node 22+ TypeScript without a build step, or when the user mentions 'native TypeScript in Node', 'strip types', 'Node 22 TypeScript', '.ts files without compilation', 'ts-node alternative', or needs guidance on error handling, graceful shutdown, flaky tests, profiling, or environment configuration in Node.js. Helps configure tsconfig.json for type stripping, set up package.json scripts, handle module resolution and import extensions, and apply robust patterns across the full Node.js stack.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 6.3 KB |
| rules/assets/graceful-server.test.ts | script | 2.6 KB |
| rules/assets/graceful-server.ts | script | 2.2 KB |
| rules/async-patterns.md | markdown | 3.1 KB |
| rules/caching.md | markdown | 4.8 KB |
| rules/environment.md | markdown | 6.0 KB |
| rules/error-handling.md | markdown | 3.8 KB |
| rules/flaky-tests.md | markdown | 10.4 KB |
| rules/graceful-shutdown.md | markdown | 4.6 KB |
| rules/logging.md | markdown | 4.4 KB |
| rules/modules.md | markdown | 2.0 KB |
| rules/node-modules-exploration.md | markdown | 3.6 KB |
| rules/performance.md | markdown | 2.8 KB |
| rules/profiling.md | markdown | 3.9 KB |
| rules/streams.md | markdown | 5.1 KB |
| rules/stuck-processes-and-tests.md | markdown | 3.9 KB |
| rules/testing.md | markdown | 4.4 KB |
| rules/typescript.md | markdown | 5.5 KB |
| tile.json | config | 1017 B |
rules/performance.md
View on GitHub---
name: performance
description: Performance optimization techniques
metadata:
tags: performance, optimization, memory, worker-threads
---
# Performance in Node.js
## Avoid Blocking the Event Loop
Never perform CPU-intensive operations synchronously:
```typescript
// BAD - blocks event loop
function hashPasswordSync(password: string): string {
return crypto.pbkdf2Sync(password, salt, 100000, 64, 'sha512').toString('hex');
}
// GOOD - async operation
function hashPassword(password: string): Promise<string> {
return new Promise((resolve, reject) => {
crypto.pbkdf2(password, salt, 100000, 64, 'sha512', (err, key) => {
if (err) reject(err);
else resolve(key.toString('hex'));
});
});
}
```
## Worker Threads with Piscina
Use [piscina](https://github.com/piscinajs/piscina) for CPU-intensive tasks:
```typescript
// worker.ts
export default function heavyComputation(data: { input: string }): string {
// CPU-intensive work here
return result;
}
```
```typescript
// main.ts
import Piscina from 'piscina';
const piscina = new Piscina({
filename: new URL('./worker.ts', import.meta.url).href,
});
const result = await piscina.run({ input: 'data' });
```
Piscina handles worker pool management, task queuing, and load balancing automatically.
## Connection Pooling
Always use connection pools for databases:
```typescript
import { Pool } from 'pg';
const pool = new Pool({
max: 20,
idleTimeoutMillis: 30000,
connectionTimeoutMillis: 2000,
});
async function query<T>(sql: string, params: unknown[]): Promise<T[]> {
const client = await pool.connect();
try {
const result = await client.query(sql, params);
return result.rows;
} finally {
client.release();
}
}
```
## Avoid Memory Leaks
Common memory leak patterns to avoid:
```typescript
// BAD - unbounded cache
const cache = new Map();
function addToCache(key: string, value: unknown) {
cache.set(key, value); // Never cleaned up
}
// GOOD - LRU cache with max size
import { LRUCache } from 'lru-cache';
const cache = new LRUCache<string, unknown>({
max: 500,
ttl: 1000 * 60 * 5,
});
// BAD - listener leak
function subscribe(emitter: EventEmitter) {
emitter.on('event', handler); // Never removed
}
// GOOD - cleanup listeners
function subscribe(emitter: EventEmitter): () => void {
emitter.on('event', handler);
return () => emitter.off('event', handler);
}
```
## Lazy Loading
Load modules only when needed:
```typescript
let heavyModule: HeavyModule | null = null;
async function getHeavyModule(): Promise<HeavyModule> {
if (!heavyModule) {
const { HeavyModule } = await import('./heavy-module.js');
heavyModule = new HeavyModule();
}
return heavyModule;
}
```
## Related
- [caching.md](./caching.md) - Caching patterns and libraries
- [profiling.md](./profiling.md) - Profiling and benchmarking tools
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |