
A general-purpose discrete event simulation (DES) framework for Node.js, written in TypeScript.
Simloop provides a minimal, type-safe API for building simulations of real-world systems. You define events, entities, and handlers — the framework runs the event loop.
Why Simloop?
- Type-safe — generic
TEventMapgives full autocomplete and type checking on event scheduling and handling - Zero runtime dependencies — only Node.js built-ins
- Deterministic — seeded PRNG ensures reproducible results
- Simple API — define handlers with
sim.on(), schedule events withctx.schedule() - Built-in statistics — online mean, variance, min, max, count
- Pluggable logging — bring your own logger or use the default console logger
- Dual module format — ESM and CJS
Quick Start
npm install simloopimport { SimulationEngine } from 'simloop';
// 1. Define your event types
type Events = {
'customer:arrive': { customerId: string };
'customer:serve': { customerId: string };
};
// 2. Create the engine
const sim = new SimulationEngine<Events>({ seed: 42, maxTime: 100 });
// 3. Register handlers
sim.on('customer:arrive', (event, ctx) => {
ctx.stats.increment('arrivals');
ctx.schedule('customer:serve', ctx.clock + 2, {
customerId: event.payload.customerId,
});
const nextArrival = ctx.dist.exponential(0.2)();
ctx.schedule('customer:arrive', ctx.clock + nextArrival, {
customerId: `C${ctx.stats.get('arrivals').count + 1}`,
});
});
sim.on('customer:serve', (event, ctx) => {
ctx.stats.increment('served');
});
// 4. Initialize and run
sim.init((ctx) => {
ctx.schedule('customer:arrive', 0, { customerId: 'C1' });
});
const result = sim.run();
console.log(result.stats);Last updated on