A high-performance diff library for TypeScript, ported from the Rust imara-diff.
- ๐ High Performance: Optimized Histogram algorithm, 10-100% faster than Myers
- ๐ Type Safe: Strict TypeScript with branded types
- ๐ก๏ธ Memory Safe: Generation-based validation prevents use-after-free
- โ Well Tested: 108 tests with ~1,450 property test runs
- ๐ Line-based Diffs: Perfect for text files and source code
- ๐ฏ Fallback Strategy: SimpleMyers handles repetitive content
pnpm add imara-diffimport { Diff, Algorithm, InternedInput, StringLines } from 'imara-diff';
const before = "line 1\nline 2\nline 3\n";
const after = "line 1\nmodified\nline 3\n";
// Create interned input
const input = InternedInput.new(
new StringLines(before),
new StringLines(after)
);
// Compute diff
const diff = Diff.compute(Algorithm.Histogram, input);
// Get results
console.log(`Changes: +${diff.countAdditions()}/-${diff.countRemovals()}`);
// Iterate over changes
for (const hunk of diff.hunks()) {
console.log(`Changed: lines ${hunk.before.start}-${hunk.before.end}`);
}typescript/
โโโ src/
โ โโโ core/ # Core types and utilities
โ โ โโโ types.ts # Branded types, assertions
โ โ โโโ util.ts # Utility functions
โ โ โโโ sources.ts # TokenSource implementations
โ โ โโโ intern.ts # Token interning system
โ โโโ algorithms/ # Diff algorithms
โ โ โโโ histogram.ts # Histogram algorithm (primary)
โ โ โโโ myers-simple.ts # SimpleMyers (fallback)
โ โ โโโ list-pool.ts # Memory pool allocator
โ โโโ api/ # Public API
โ โ โโโ diff.ts # Main Diff class
โ โโโ index.ts # Public exports
โโโ tests/
โ โโโ unit/ # Unit tests
โ โโโ integration/ # Integration tests
โ โโโ property/ # Property-based tests
โโโ docs/ # Documentation
โ โโโ examples/ # Usage examples
โ โโโ SUMMARY.md # Technical overview
โ โโโ TESTING.md # Testing guide
โ โโโ ...
โโโ benchmarks/ # Performance benchmarks
โโโ package.json
# Install dependencies
pnpm install
# Run tests
pnpm test
# Run tests in watch mode
pnpm test --watch
# Run with coverage
pnpm test --coverage
# Build
pnpm build
# Lint
pnpm lint
# Type check
pnpm typecheck| Scenario | Size | Time | Status |
|---|---|---|---|
| Small file | 50 lines | < 50ms | โ |
| Medium file | 500 lines | < 200ms | โ |
| Large file | 2,000 lines | < 1s | โ |
Expected: 3-5x slower than Rust (acceptable for TypeScript)
108 tests with comprehensive coverage:
- โ 35 unit tests
- โ 45 edge case tests
- โ 15 property tests (~1,450 runs)
- โ 13 integration tests
See TESTING.md for details.
- SUMMARY.md - Technical overview and architecture
- TESTING.md - Testing guide and strategies
- TEST_COVERAGE_REPORT.md - Coverage analysis
- PROGRESS.md - Implementation progress
- FINAL_STATUS.md - Project completion status
class Diff {
static compute<T>(algorithm: Algorithm, input: InternedInput<T>): Diff;
countAdditions(): number;
countRemovals(): number;
isRemoved(idx: number): boolean;
isAdded(idx: number): boolean;
hunks(): IterableIterator<Hunk>;
getAllHunks(): Hunk[];
}class InternedInput<T> {
static new<T>(before: TokenSource<T>, after: TokenSource<T>): InternedInput<T>;
before: Token[];
after: Token[];
interner: Interner<T>;
}enum Algorithm {
Histogram = 'histogram' // Primary algorithm with fallback
}See docs/examples/ for more examples:
- Basic usage
- Real-world scenarios
- Performance benchmarks
- Error handling
Contributions welcome! Please:
- Fork the repository
- Create a feature branch
- Add tests for new features
- Ensure all tests pass
- Submit a pull request
Apache-2.0 (same as original Rust version)
Ported from imara-diff by Pascal Kuthe.
โ Production Ready
- Core functionality complete
- Comprehensive test coverage (108 tests)
- Well documented
- Type and memory safe
Optional features (not required for core functionality):
- Postprocessing with slider heuristics
- UnifiedDiff output format
- Additional algorithms
- Performance optimizations
Made with โค๏ธ in TypeScript