diff-native
v0.1.0
Published
High-performance text diffing library built with Rust and WebAssembly
Maintainers
Readme
diff-native
High-performance text diffing library built with Rust and WebAssembly. This library is a drop-in replacement for the popular jsdiff library, offering identical API with dramatically improved performance and reduced memory usage.
Features
- ⚡ Blazing Fast: 10-20x faster than JavaScript diffing libraries
- 💾 Memory Efficient: Up to 80% less memory usage
- 🔄 Drop-in Replacement: Identical API to the original jsdiff library
- 📝 Text Diffing: Compare strings at character, word, line, and sentence levels
- 🔍 Patching: Create, parse, and apply patches in unified diff format
- 🌐 Universal: Works in Node.js, Bun, and modern browsers
- 📦 Small Footprint: Optimized WASM binary for minimal bundle size
- 🦀 Rust-Powered: Built with Rust for maximum performance and safety
Installation
# NPM
npm install diff-native
# Yarn
yarn add diff-native
# PNPM
pnpm add diff-native
# Bun
bun add diff-nativeUsage
Node.js (CommonJS)
const { diffChars, diffWords, diffLines } = require('diff-native');
// Basic character-level diff
const charChanges = diffChars('old string', 'new string');
// Word-level diff
const wordChanges = diffWords('old string with words', 'new string with words');
// Line-level diff
const lineChanges = diffLines('line one\nline two', 'line one\nmodified line\nline two');
// Each change has properties:
// - value: The text content
// - added: true if this is an addition
// - removed: true if this is a removal
// - count: Number of tokens in the changeBun or ESM
import { diffChars, diffWords, diffLines } from 'diff-native';
// Use the same API as in the CommonJS example
const changes = diffChars('old string', 'new string');Browser via CDN
<script type="module">
import * as diff from 'https://cdn.jsdelivr.net/npm/diff-native/dist/browser/index.js';
const changes = diff.diffChars('old string', 'new string');
console.log(changes);
</script>API Overview
Diff Methods
All diff methods follow the same basic pattern:
diff(oldText, newText, options?)- diffChars: Character by character diff
- diffWords: Word level diff
- diffWordsWithSpace: Word level diff including whitespace
- diffLines: Line level diff
- diffTrimmedLines: Line level diff with whitespace trimming
- diffSentences: Sentence level diff
- diffCss: CSS specific diff
- diffJson: JSON diff that handles objects
Options
// Example options
const options = {
ignoreCase: true, // Ignore case differences
ignoreWhitespace: true, // Ignore whitespace differences
oneChangePerToken: false // Combine consecutive changes of the same type
};Patch Methods
// Create a patch
const patch = createPatch('filename.txt', oldText, newText);
// Apply a patch
const patched = applyPatch(oldText, patch);
// Parse a patch
const parsed = parsePatch(patchString);Line Ending Utilities
// Check line ending style
const isUnixEndings = isUnix(patchString);
const isWindowsEndings = isWin(patchString);
// Convert between line endings
const unixPatch = winToUnix(patchString);
const windowsPatch = unixToWin(patchString);Performance
diff-native dramatically outperforms other JavaScript diffing libraries:
- 10-20x faster for text comparisons across all diff operations
- Up to 80% less memory usage compared to pure JavaScript implementations
- Consistent performance gains across Node.js, Bun, and browser environments
- Optimized for all diff operations - character, word, line, sentence, and JSON diffs all benefit
Performance benefits are most noticeable when processing larger documents or working with resource-constrained environments.
Browser Compatibility
diff-native works in all modern browsers that support WebAssembly:
- Chrome 57+
- Firefox 53+
- Safari 11+
- Edge 16+
Runtime Support
- Node.js: 14.0.0+
- Bun: All versions
- Deno: With npm compatibility mode
Module Formats
This package provides multiple build formats:
- CommonJS for Node.js (default)
- ES Modules for bundlers and modern runtimes
- Browser-ready bundle
Development
Prerequisites
Building
# Install dependencies
bun install
# Build all targets (node, browser, and esm)
bun run build
# Build specific targets
bun run build:wasm:node # Only build Node.js target
bun run build:wasm:web # Only build Web targetTesting
# Run all tests
bun test
# Run tests after building
bun run build && bun test
# Run tests in watch mode
bun test --watch
# Run Rust unit tests
bun run test:rust
# or
cargo test --lib
# Run WebAssembly integration tests
wasm-pack test --nodeBuild Outputs
The build process creates several distribution formats:
dist/node/- CommonJS modules for Node.jsdist/browser/- Browser-compatible modulesdist/esm/- ES modules for modern bundlers
Code Quality
This project uses several tools to maintain code quality:
# Run ESLint to check code
bun run lint
# Fix ESLint issues automatically
bun run lint:fix
# Format code with Prettier
bun run format
# Check formatting without changing files
bun run format:check
# Run both linting and format checking
bun run check
# Fix all linting and formatting issues
bun run fixHusky and lint-staged are configured to automatically check code quality before each commit.
Issues and Feature Requests
If you encounter any bugs or have feature requests, please file an issue on GitHub.
Pull Requests
Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.
Make sure to:
- Follow the code style guidelines
- Add or update tests as necessary
- Update documentation for any API changes
- Maintain API compatibility with the original jsdiff library
Versioning
This project follows Semantic Versioning. The version numbers follow the pattern: MAJOR.MINOR.PATCH.
