Skip to content

clarkezyz/diff-viewer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Diff Viewer

Visual text comparison tool with word-level precision highlighting.

A clean, fast, client-side diff viewer for comparing text documents. Perfect for code review, document versioning, or spotting changes between any two text blocks.


Features

🔬 Word-Level Precision

Unlike basic line-by-line diff tools, Diff Viewer highlights exactly which words changed within modified lines:

- The quick brown fox jumps
+ The quick red fox leaps

Shows brownred and jumpsleaps highlighted inline, not just the full lines in red/green.

Real-Time Updates

Diff updates as you type. No "Compare" button needed. Instant visual feedback.

🔒 100% Client-Side

  • No server uploads - All processing happens in your browser
  • No tracking - Zero analytics, zero data collection
  • No accounts - Just open and use
  • Works offline - Save the HTML file, use anywhere

🎯 Professional Algorithm

Uses the industry-standard jsdiff library (same algorithm as git diff) with a fallback for environments where the CDN is blocked.


Quick Start

Use Online

  1. Open index.html in any modern browser
  2. Paste your original text in the left box
  3. Paste your modified text in the right box
  4. Visual diff appears automatically below

Run Locally

git clone https://github.com/clarkezyz/diff-viewer.git
cd diff-viewer
# Open index.html in your browser

No build process. No dependencies to install. Just open the file.


Use Cases

Code Review

Compare versions of a function before/after refactoring:

// Original
function getUserData(id) {
  return fetch('/api/users/' + id).then(r => r.json());
}

// Modified
async function getUserData(userId) {
  const response = await fetch(`/api/users/${userId}`);
  return response.json();
}

What Diff Viewer shows:

  • Parameter renamed: iduserId
  • Template literal instead of concatenation
  • async/await instead of .then()

All highlighted at the word level, not just "line changed."

Document Versioning

Track changes to contracts, specifications, or documentation:

Original: "Contractor shall deliver within 30 days"
Modified: "Contractor must deliver within 15 business days"

Highlights:

  • shallmust (legal language strengthening)
  • 30 days15 business days (timeline change)

Debugging Config Files

Compare production vs. development config:

// Dev
{"timeout": 5000, "retries": 3, "debug": true}

// Prod
{"timeout": 30000, "retries": 5, "debug": false}

See exactly which values changed without mental diffing.

Content Editing

Writers, editors, journalists comparing article drafts:

  • See which sentences were added or removed
  • Track word-level edits within paragraphs
  • Verify copy edits didn't change meaning

How It Works

Algorithm

  1. Line-Level Diff: Split both texts into lines, compare using Myers diff algorithm (via jsdiff)
  2. Modification Detection: When a line is removed AND a line is added consecutively, treat as modification
  3. Word-Level Diff: For modified lines, run word-level diff to show inline changes
  4. Rendering: Display with color coding:
    • Green = Added content
    • Red = Removed content
    • Gray = Unchanged content
    • Inline highlighting = Word-level changes

Fallback Mode

If the jsdiff CDN is unavailable (blocked, offline, etc.), Diff Viewer uses a simple line-by-line comparison. Less precise, but still functional.

Performance

  • Real-time diffing - No lag on typical documents (< 10,000 lines)
  • Client-side only - No network latency
  • Lightweight - ~250 lines of JavaScript, ~200 lines of CSS

Technical Details

Dependencies

  • jsdiff v5.1.0 (CDN) - Professional diff algorithm
    • URL: https://cdnjs.cloudflare.com/ajax/libs/jsdiff/5.1.0/diff.min.js
    • Fallback mode if unavailable
  • Google Fonts (Inter, JetBrains Mono) - Typography
    • Can be replaced with local fonts for offline use

Browser Compatibility

  • Chrome/Edge - Full support
  • Firefox - Full support
  • Safari - Full support
  • Mobile browsers - Works, but desktop recommended for side-by-side view

Requirements:

  • ES6 JavaScript support (any browser from 2017+)
  • No backend required

File Structure

diff-viewer/
├── index.html       # Main application
├── diff.js          # Diff algorithm and logic
├── diff.css         # Styling
└── README.md        # This file

Why This Tool Exists

The Problem with Most Diff Tools

  1. Line-level only: Show full lines as changed, not which words
  2. Require uploads: Send your text to a server (privacy concern)
  3. Cluttered UI: Ads, tracking, complex interfaces
  4. No real-time: Need to click "Compare" button repeatedly

Why Diff Viewer is Different

  • Word-level precision: See exactly what changed
  • No uploads: Everything in your browser
  • Clean interface: Two text boxes, one result. That's it.
  • Real-time: Updates as you type

Built for developers, writers, and anyone who needs to compare text quickly and privately.


Privacy & Security

What This Tool Does NOT Do

  • ❌ Upload your text to any server
  • ❌ Store your text in cookies/localStorage (unless you save the page)
  • ❌ Track your usage
  • ❌ Collect analytics
  • ❌ Show ads
  • ❌ Require accounts or login

What This Tool DOES Do

  • ✅ Process everything locally in your browser
  • ✅ Work completely offline (after first load)
  • ✅ Let you save the HTML file and use it anywhere
  • ✅ Use only one external CDN (jsdiff) with a fallback

For maximum privacy: Download the repo, host it locally, and replace the jsdiff CDN with a local copy.


Development

Architecture Principles

  • No build process - Open the HTML file, it works
  • Vanilla JavaScript - No frameworks, no npm packages
  • Progressive enhancement - Works without jsdiff (fallback mode)
  • Responsive design - Mobile-friendly (though desktop is ideal)

Code Style

  • ES6 class-based architecture
  • Inline comments for algorithm steps
  • Clean separation: HTML structure, CSS styling, JS logic
  • Professional diff algorithm with fallback

FAQ

Why use this instead of git diff or IDE diff tools?

  • Quick comparisons: No need to commit files or open an IDE
  • Non-code text: Works on contracts, articles, documentation
  • Visual interface: Easier to read than terminal output
  • Shareable: Send someone the HTML file, they can use it instantly

Can I compare code files directly?

Yes, but you'll need to copy/paste the code into the text boxes. This tool doesn't read files from disk (browser security limitation).

Alternative workflow:

cat file1.js | pbcopy  # macOS - copy to clipboard
# Paste into left box
cat file2.js | pbcopy
# Paste into right box

Does this work offline?

Yes, after the first load. The only external resource is the jsdiff CDN. If that's blocked, the fallback algorithm activates.

For 100% offline:

  1. Download jsdiff.min.js locally
  2. Change the script src in index.html to <script src="jsdiff.min.js"></script>
  3. Now it works with zero network access

Why is word-level diffing better?

Line-level diff:

- The quick brown fox
+ The quick red fox

You see two lines changed. You mentally parse which words are different.

Word-level diff:

- The quick brown fox
+ The quick red fox
        (brown → red highlighted)

You immediately see brownred is the only change.

What's the maximum file size?

Practical limit: ~10,000 lines before performance degrades. For massive files (100k+ lines), use command-line tools like diff or git diff.

Why the limit: Real-time diffing on every keystroke requires fast processing. For huge files, disable real-time and add a "Compare" button.

Can I embed this in my website?

Yes! MIT license. Use it however you want:

  • Embed in your app
  • Customize the UI
  • Add features
  • Remove features

Attribution appreciated but not required.


Contributing

Contributions welcome! This project values:

  • Simplicity - No complex build processes
  • Privacy - No tracking, ever
  • Performance - Fast diffing, real-time updates
  • Accessibility - Clean, readable output

Ideas for Contribution

  • Character-level diff mode (for tiny changes)
  • Copy diff output to clipboard (formatted)
  • Dark/light theme toggle
  • Syntax highlighting for code
  • File upload support (drag/drop)
  • Diff statistics (lines added/removed/changed)
  • Export diff as patch file
  • Keyboard shortcuts (Ctrl+1/2 to focus text boxes)

License

MIT License - See LICENSE file for details

Free for personal and commercial use. Attribution appreciated but not required.


Credits

Built by Clarke Zyz - 2025

Because comparing text shouldn't require uploading your data to someone else's server.

Inspirations

  • The excellent jsdiff library
  • GitHub's diff viewer
  • Developers debugging config file changes at 2am
  • Writers tracking article edits

Alternatives

If Diff Viewer doesn't fit your needs, check out:

  • command-line: diff, git diff, vimdiff
  • web-based (server): diffchecker.com, text-compare.com
  • IDE-integrated: VSCode diff, IntelliJ diff viewer
  • advanced: Beyond Compare, WinMerge, Meld

Diff Viewer's niche: Quick, visual, private, client-side text comparison.


Diff Viewer - Because sometimes you just need to see what changed.

Compare • Highlight • Understand

About

Visual text comparison tool with word-level precision highlighting.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •