Thanks to visit codestin.com
Credit goes to github.com

Skip to content

DamienMonchaty/BlitzJS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

BlitzJS ⚑

Ultra-fast HTTP framework with simple, intuitive API built on uWebSockets.js

⚠️ Development Status: BlitzJS is currently in active development. The API may change before the stable release. Not recommended for production use yet.

Features

  • ⚑ Lightning Fast - Built on uWebSockets.js, one of the fastest HTTP servers
  • 🎯 Simple & Clean API - Intuitive, chainable syntax for maximum productivity
  • πŸ”§ TypeScript First - Full TypeScript support with excellent type inference
  • πŸ“¦ Zero Dependencies - Only uWebSockets.js as peer dependency
  • πŸš€ Auto Response - Automatic JSON/string response handling

Installation

Note: BlitzJS is not yet published on npm. This is a development preview.

# When available on npm (coming soon)
npm install blitzjs

For now, you can clone and build locally:

git clone <repository-url>
cd BlitzJS
npm install
npm run build

Quick Start

import { BlitzJS } from 'blitzjs';

new BlitzJS()
  .get('/', 'Hello BlitzJS!')
  .get('/json', { message: 'Auto JSON response!' })
  .get('/user/:id', (ctx) => ({ 
    id: ctx.params.id, 
    name: `User ${ctx.params.id}` 
  }))
  .listen(3000);

API

Simple Responses

// String response
.get('/', 'Hello World!')

// JSON response  
.get('/data', { key: 'value' })

// Function response
.get('/time', () => new Date().toISOString())

// Dynamic response
.get('/user/:id', (ctx) => ({ id: ctx.params.id }))

HTTP Methods

new BlitzJS()
  .get('/users', () => getAllUsers())
  .post('/users', (ctx) => createUser(ctx.body))
  .put('/users/:id', (ctx) => updateUser(ctx.params.id))
  .delete('/users/:id', (ctx) => deleteUser(ctx.params.id))
  .listen(3000);

Context

Route handlers receive a context object:

interface RouteContext {
  req: HttpRequest;      // uWebSockets.js request
  res: HttpResponse;     // uWebSockets.js response  
  params: Record<string, string>;  // Route parameters
  query: Record<string, string>;   // Query parameters
  body?: any;           // Request body (if parsed)
}

Middleware

import { BlitzJS } from 'blitzjs';

const app = new BlitzJS()
  .use(async (ctx, next) => {
    console.log(`${ctx.req.getMethod()} ${ctx.req.getUrl()}`);
    await next();
  })
  .get('/', 'Hello with middleware!')
  .listen(3000);

Factory Function

import { Blitz } from 'blitzjs';

// Use the factory function for a more functional approach
const app = Blitz()
  .get('/', 'Hello from factory!')
  .listen(3000);

Performance

BlitzJS leverages uWebSockets.js and advanced optimizations to deliver exceptional performance:

πŸ”₯ Ultra-Performance Features

  • Template Pattern Handler Generation - Eliminates closures, reuses optimized templates
  • O(1) Static Route Lookup - HashMap-based routing for static routes
  • O(log n) Dynamic Route Lookup - Optimized trie-based routing for dynamic routes
  • Runtime Code Generation - JIT-compiled handlers for maximum performance
  • Pre-computed Headers - Eliminates header computation overhead
  • Ultra-Fast Router - Compiled dispatch function for minimal overhead

πŸ“Š Benchmark Results

Note: These are preliminary benchmarks from development testing. Official benchmarks will be published with the stable release.

Latest performance validation with Template Pattern optimizations:

Route Type Req/s Latency P50 Optimization
Static String 66,005 13ms Template + O(1) HashMap
Static JSON 60,370 15ms Template + O(1) HashMap
Dynamic Single Param 55,806 14ms Template + Optimized Regex
Dynamic Multi Param 48,547 16ms Template + Complex Regex

Tested with autocannon: 100 connections, 10 pipelining, 10s duration

πŸš€ Performance Optimizations

  • Template Pattern: Handler templates compiled once, reused without closures
  • Static Route HashMap: O(1) lookup for static routes
  • Dynamic Route Trie: O(log n) lookup with optimized regex patterns
  • Pre-computed Responses: Headers and common responses cached
  • JIT Compilation: Runtime code generation for optimal V8 optimization

Template Pattern Architecture

BlitzJS uses the Template Pattern for handler generation, achieving maximum performance by eliminating closures and enabling optimal V8 optimization:

Traditional Closure-based Approach

// ❌ Creates closures for each handler - less optimal
const createHandler = (response) => {
  return (ctx) => {
    ctx.res.end(response); // Closure captures response
  };
};

BlitzJS Template Pattern Approach

// βœ… Reusable templates without closures - ultra-optimized
const stringTemplate = function(ctx, precomputedResponse, precomputedHeaders) {
  ctx.res.writeHeader('content-type', 'text/plain; charset=utf-8');
  ctx.res.end(precomputedResponse);
};

// All handlers use the same optimized template function

Benefits

  • Zero Closures - Templates are reused, no closure overhead
  • V8 Optimization - Templates get heavily optimized by V8 JIT
  • Memory Efficiency - Single template function for all similar handlers
  • Pre-computed Data - Responses and headers computed at compile time
  • Maximum Performance - Benchmarks show ~69,000 req/s throughput

Roadmap

🚧 In Development

  • Request body parsing (JSON, form-data, etc.)
  • Query string parsing
  • Cookie support
  • Session management
  • Static file serving
  • Error handling middleware
  • CORS support
  • Rate limiting
  • WebSocket support

🎯 Planned Features

  • Plugin system
  • Request validation
  • Authentication helpers
  • Database integrations
  • OpenAPI/Swagger support
  • Testing utilities
  • Performance monitoring
  • Clustering support

Contributing

BlitzJS is in active development and we welcome contributions!

Development Setup

git clone <repository-url>
cd BlitzJS
npm install
npm run dev

Areas Where Help is Needed

  • πŸ§ͺ Testing: Writing comprehensive test suites
  • πŸ“š Documentation: Improving examples and guides
  • ⚑ Performance: Benchmarking and optimization
  • πŸ”§ Features: Implementing roadmap items
  • πŸ› Bug Reports: Finding and reporting issues

Examples

See the example/ directory:

  • blitz-style.ts - Complete BlitzJS example with various features

License

MIT


BlitzJS - When you need speed ⚑

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published