Skip to content

CraftFast Immutable State

The fastest immutable state library for TypeScript. 1.4-35x faster than immer, 4.6 KB gzipped, zero dependencies, 100% type-safe.

Craft

Quick Start

bash
# Install
npm install @sylphx/craft
typescript
import { craft } from "@sylphx/craft";

const baseState = {
  user: { name: "Alice", age: 25 },
  todos: [
    { id: 1, text: "Learn Craft", done: false },
    { id: 2, text: "Use Craft", done: false },
  ],
};

const nextState = craft(baseState, (draft) => {
  draft.user.age = 26;
  draft.todos[0].done = true;
  draft.todos.push({ id: 3, text: "Master Craft", done: false });
});

// Original is unchanged
console.log(baseState.user.age); // 25

// New state has the updates
console.log(nextState.user.age); // 26
console.log(nextState.todos.length); // 3

// Structural sharing - unchanged parts are the same reference
console.log(baseState.todos[1] === nextState.todos[1]); // true

Performance Highlights

Performance Comparison

Based on comprehensive real-world benchmarks (3 runs, statistically validated)

Core Operations

ScenarioCraft vs immerWinner
Simple object updates1.44-1.57x faster🏆 Craft
Nested updates (3-5 levels)1.48-1.69x faster🏆 Craft
Complex state updates1.08-1.15x faster🏆 Craft
Structural sharing1.33-1.46x faster🏆 Craft
No-op detection1.21-1.27x faster🏆 Craft

Map/Set Operations

ScenarioCraft vs immerWinner
Map.set()2.67-3.48x faster🏆 Craft
Map.delete()3.15-3.34x faster🏆 Craft
Set.add()6.13-7.60x faster🏆 Craft
Set.delete()5.83-5.94x faster🏆 Craft
Large Set (100 items)33-35x faster🏆 Craft

JSON Patches

ScenarioCraft vs immerWinner
Generate patches1.39-1.77x faster🏆 Craft
Apply patches24-25x faster 🚀🏆 Craft
Undo/Redo2.15-2.28x faster🏆 Craft

Craft wins in 95% of real-world scenarios!

Why Craft?

vs Manual Immutable Updates

Stop the spread operator madness:

typescript
// ❌ Manual (error-prone, verbose, slow)
const nextState = {
  ...state,
  user: {
    ...state.user,
    profile: {
      ...state.user.profile,
      age: state.user.profile.age + 1,
    },
  },
};

// ✅ Craft (simple, safe, fast)
const nextState = craft(state, (draft) => {
  draft.user.profile.age++;
});

vs Immer

Craft is immer, but better in every way:

FeatureCraftimmer
Performance1.4-35x fasterBaseline
Bundle Size4.6 KB gzipped~13 KB gzipped
API Coverage100% compatible
TypeScriptPerfect inferenceGood
Map/Set Support✓ 3-35x faster✓ Full support
JSON Patches✓ 1.6-24x faster✓ RFC 6902
CompositionRich functional APIBasic
Custom Shallow Copy✓ Advanced API❌ No
Debugging Tools✓ 9 utilitiesBasic
DependenciesZeroMultiple

What Makes Craft Fast?

  1. Zero WeakMap overhead - Child drafts stored directly on state
  2. Optimized proxy traps - Inlined functions, minimal indirection
  3. Single-pass algorithms - Combine scanning and processing
  4. Smart caching - Eliminate redundant operations
  5. Native method reuse - Direct access, no wrappers

License

MIT © Sylphx Limited

Built with ❤️ for developers who refuse to compromise on performance.

Released under the MIT License.