Modern TypeScript Dependency Injection
Blazing fast, lightweight DI container optimized for Bun runtime.
Sub-microsecond resolution with full type safety.
$ bun add fast-injection
Sub-microsecond dependency resolution. Up to 26M ops/sec for cached singletons.
Less than 5KB minified + gzipped. No production dependencies.
Full TypeScript support with automatic type inference. Catch errors at compile time.
Protected against prototype pollution, memory leaks, ReDoS, and DoS attacks.
Singleton, transient, and scoped lifetimes for complete control over instance creation.
Choose your style: decorators, functional, or imperative. It's your choice.
Built-in testing utilities for easy mocking and test isolation.
Native performance optimizations for the Bun runtime.
Start using fast-injection in minutes with our intuitive API.
import { Container } from "fast-injection";
import { singleton, inject } from "fast-injection/decorators";
// Define services with decorators
@singleton()
class Database {
connect() { /* ... */ }
}
@singleton()
class UserService {
constructor(@inject(Database) private db: Database) {}
getUser(id: string) {
return this.db.query(`SELECT * FROM users WHERE id = ${id}`);
}
}
// Create container and register services
const container = new Container();
container.register(Database);
container.register(UserService);
// Resolve with automatic dependency injection
const userService = container.resolve(UserService);
import { Container, Lifetime } from "fast-injection";
// Define your services
class Config {
apiUrl = "https://api.example.com";
}
class ApiClient {
constructor(private config: Config) {}
}
// Create container
const container = new Container();
// Register with factory functions
container.register(Config, {
lifetime: Lifetime.Singleton
});
container.register(ApiClient, {
lifetime: Lifetime.Singleton,
factory: (c) => new ApiClient(c.resolve(Config))
});
// Resolve services
const client = container.resolve(ApiClient);
import { Container } from "fast-injection";
class Cache {
private store = new Map<string, string>();
// Called after instantiation
onInit() {
console.log("Cache initialized");
}
// Called on container/scope disposal
onDispose() {
this.store.clear();
console.log("Cache disposed");
}
set(key: string, value: string) {
this.store.set(key, value);
}
}
const container = new Container();
container.register(Cache);
const cache = container.resolve(Cache);
cache.set("foo", "bar");
// Cleanup when done
await container.dispose();
Optimized for speed with minimal overhead
| Operation | Ops/Second | Avg Time |
|---|---|---|
| Singleton (cached) | ~26M ops/sec | 0.038µs |
| Scoped (cached) | ~25M ops/sec | 0.039µs |
| Create scope | ~21M ops/sec | 0.048µs |
| Value retrieval | ~19M ops/sec | 0.052µs |
| Transient | ~11M ops/sec | 0.091µs |
| Factory | ~7.8M ops/sec | 0.13µs |
* Benchmarks run on Apple M2 Pro with Bun runtime
@singleton() for services like database connections,
configuration, and loggers. Singleton resolution is 98% faster than transient.
Everything you need to get productive
Join developers building faster, more maintainable applications with fast-injection.