Diff Viewer

Compare two texts and visualize differences — line-by-line with character-level highlighting

Original
0 lines
Modified
0 lines
Diff Result

What is a Diff Viewer?

A diff viewer (or diff tool) is a utility that compares two pieces of text and highlights the differences between them. It analyzes both inputs line by line, identifying which lines were added, removed, or modified. Diff tools are essential in software development for code review, debugging, and version control workflows.

The concept of "diff" originates from the Unix diff command, first released in 1974 as part of AT&T's Unix. Modern diff tools build on the same core algorithms — primarily the Longest Common Subsequence (LCS) algorithm — to produce minimal, human-readable change sets. Tools like Git, GitHub, GitLab, and Bitbucket all use variations of diff algorithms to display code changes in pull requests and commits.

This online diff viewer runs entirely in your browser. It computes diffs using a JavaScript implementation of the LCS algorithm, with additional character-level diffing for modified lines to show exactly which parts of a line changed. No data is ever sent to any server.

Unified vs. Side-by-Side Diff

There are two primary ways to display text differences, each with distinct advantages:

Unified Diff

Unified diff shows all changes in a single column. Removed lines are prefixed with - and added lines with +. Unchanged (context) lines appear without a prefix. This format is compact and widely used in command-line tools, patch files, and Git output (git diff). It is ideal when you want a concise overview of changes.

Side-by-Side Diff

Side-by-side diff places the original text on the left and the modified text on the right, aligned line by line. This makes it easy to visually scan changes and see the "before and after" simultaneously. Most code review platforms (GitHub, GitLab, Bitbucket, Azure DevOps) offer a side-by-side view as the default for pull request reviews.

View Mode Best For Trade-off
Unified Compact overview, patch files, terminal output Harder to compare long lines visually
Side by Side Code review, visual comparison, long documents Uses more horizontal space

Common Use Cases for Text Comparison

  • Code review — Compare two versions of a source file to understand what changed in a commit, pull request, or branch merge.
  • Configuration auditing — Diff configuration files (YAML, JSON, INI, TOML) to find unintended changes between environments (dev, staging, production).
  • Document comparison — Compare two versions of a contract, specification, or policy document to identify edits.
  • API response debugging — Compare expected vs. actual API responses to locate discrepancies.
  • Database migration verification — Diff SQL migration scripts or schema dumps to ensure changes are correct before running them.
  • Merge conflict resolution — Paste the two conflicting versions to understand the differences before resolving a Git merge conflict.
  • Log analysis — Compare log outputs from two test runs to find differences in behavior.

Features of This Diff Tool

  • Line-level diff — Compares text line by line using the Longest Common Subsequence (LCS) algorithm, the same family of algorithms used by Git and other professional diff tools.
  • Character-level highlighting — Within modified lines, individual characters that changed are highlighted with inline markers, making it easy to spot small edits in long lines.
  • Two view modes — Switch between unified diff (single column with +/- markers) and side-by-side diff (left/right panel comparison) with one click.
  • Change statistics — See at a glance how many lines were added, deleted, and unchanged, along with the total line count.
  • Swap panels — Instantly swap the original and modified text to reverse the comparison direction.
  • Copy diff output — Export the diff result as a unified diff text format, suitable for sharing or pasting into issues and documentation.
  • Sample data — Load a built-in sample to explore the tool's features without preparing your own test data.
  • Keyboard shortcut — Press Ctrl+Enter (or ⌘+Enter) to compare instantly without reaching for the mouse.
  • Resizable panels — Drag the divider between the original and modified panels to adjust their width.
  • 100% client-side — All comparison happens in your browser. No data is ever sent to any server. Works offline after the initial page load.
  • No dependencies — Built with vanilla HTML, CSS, and JavaScript. No frameworks, no build tools, no npm packages.

How the Diff Algorithm Works

This diff viewer uses a two-pass approach to compute differences:

Pass 1: Line-Level Diff (LCS)

The tool splits both texts into lines and computes the Longest Common Subsequence (LCS) — the longest sequence of lines that appear in both texts in the same order. Lines present in the LCS are marked as "unchanged." Lines in the original but not in the LCS are "deleted," and lines in the modified text but not in the LCS are "added."

The LCS algorithm runs in O(m×n) time and space, where m and n are the line counts of the two inputs. For most practical inputs (up to a few thousand lines), this executes in milliseconds.

Pass 2: Character-Level Diff (Inline Highlighting)

When a deleted line is immediately followed by an added line (suggesting a modification rather than a pure addition/deletion), the tool runs a second LCS pass at the character level. This highlights exactly which characters within the line changed, making small edits (typo fixes, variable renames, parameter additions) immediately visible.

For very long lines (over ~700 characters), the character-level diff falls back to highlighting the entire line to avoid performance issues.

Frequently Asked Questions

What is a diff viewer?

A diff viewer is a tool that compares two pieces of text and highlights the differences between them. It shows which lines were added, removed, or modified, making it easy to understand what changed between two versions of a document or code file.

What is the difference between unified and side-by-side diff?

Unified diff shows all changes in a single column with + and - markers for added and removed lines. Side-by-side diff shows the original text on the left and the modified text on the right, aligned line-by-line. Unified is more compact; side-by-side is easier to visually scan.

Is this diff tool safe for comparing sensitive data?

Yes. All text comparison happens entirely in your browser using JavaScript. No data is ever sent to any server. You can verify this by checking the browser's Network tab — no requests are made when you click Compare. The tool works offline after the initial page load.

What diff algorithm does this tool use?

This tool uses a Longest Common Subsequence (LCS) algorithm to compute line-level diffs, and a character-level LCS for inline highlighting within modified lines. This produces clean, minimal diffs similar to what you see in Git and code review tools.

Can I compare code files with this tool?

Yes. This diff viewer works with any plain text, including source code in any programming language, configuration files (JSON, YAML, TOML, INI), SQL scripts, Markdown, and more. It uses a monospace font for accurate alignment.

How large of a file can I compare?

The tool handles texts up to several thousand lines comfortably in most browsers. For very large files (10,000+ lines), the comparison may take a few seconds depending on your device. All processing happens in your browser's memory, so available RAM is the practical limit.

What does the character-level highlighting show?

When a line is modified (not purely added or deleted), the diff viewer highlights the specific characters that changed within that line. Deleted characters are shown in red and added characters in green, making it easy to spot small edits like typos, variable renames, or parameter changes.