fast-injection

Modern TypeScript Dependency Injection

Blazing fast, lightweight DI container optimized for Bun runtime.
Sub-microsecond resolution with full type safety.

$ bun add fast-injection

Why fast-injection?

Blazing Fast

Sub-microsecond dependency resolution. Up to 26M ops/sec for cached singletons.

🪶

Lightweight

Less than 5KB minified + gzipped. No production dependencies.

🔒

Type-Safe

Full TypeScript support with automatic type inference. Catch errors at compile time.

🛡️

Secure

Protected against prototype pollution, memory leaks, ReDoS, and DoS attacks.

🔄

Multiple Lifetimes

Singleton, transient, and scoped lifetimes for complete control over instance creation.

🎨

Flexible API

Choose your style: decorators, functional, or imperative. It's your choice.

🧪

Testing Friendly

Built-in testing utilities for easy mocking and test isolation.

🔥

Bun Optimized

Native performance optimizations for the Bun runtime.

Getting Started

Start using fast-injection in minutes with our intuitive API.

TypeScript
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);
TypeScript
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);
TypeScript
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();

Performance Benchmarks

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

💡
Performance Tip: Use @singleton() for services like database connections, configuration, and loggers. Singleton resolution is 98% faster than transient.

Documentation

Everything you need to get productive

📄

Quick Start Guide

Get up and running with fast-injection in minutes. Perfect for beginners.

📘

Examples

Comprehensive examples covering all features and use cases.

🛡️

Security Guide

Learn about security features and best practices.

Ready to supercharge your DI?

Join developers building faster, more maintainable applications with fast-injection.

Star on GitHub View on npm