1. Purpose

The DOM (Document Object Model) algorithm is Canon’s default, stable algorithm for document comparison. It provides fast, position-based comparison with traditional diff output.

This page explains when to use the DOM algorithm, how it works at a high level, and how to configure it effectively.

2. When to Use

The DOM algorithm is Canon’s recommended algorithm for production use.

2.1. Use DOM Algorithm When

  • ✓ You need stable, well-tested comparison for production environments

  • ✓ You want traditional line-by-line diff output

  • ✓ Documents are similar in structure with minimal rearrangement

  • ✓ You need maximum performance for large documents (> 10KB)

  • ✓ You want consistent, predictable behavior

  • ✓ You’re comparing any document format (XML, HTML, JSON, YAML)

2.2. Characteristics

Feature DOM Algorithm

Status

Stable, production-ready

Performance

Fast - O(n) linear time

Memory Usage

Low - line-by-line processing

Matching Strategy

Position-based element comparison

Move Detection

No (moves shown as DELETE + INSERT)

Output Format

Line-based differences

Best For

Similar documents, traditional diffs

Document Size

Handles large documents well (> 100KB)

3. How It Works

The DOM algorithm compares documents in a straightforward manner:

3.1. High-Level Process

  1. Parse Documents - Convert both documents to DOM trees

  2. Compare Positions - Compare elements at each position in the tree

  3. Apply Match Options - Use configured dimensions for comparison

  4. Generate Differences - Create line-based diff output

3.2. Position-Based Matching

The DOM algorithm compares elements by their position in the document:

Example 1. Position-based comparison example
<!-- Document 1 -->
<book>
  <title>Canon Guide</title>
  <author>Alice</author>
</book>

<!-- Document 2 -->
<book>
  <title>Canon Guide</title>
  <author>Bob</author>
</book>

The algorithm compares: * Position 1: <book> vs <book> ✓ Match * Position 2: <title>Canon Guide</title> vs <title>Canon Guide</title> ✓ Match * Position 3: <author>Alice</author> vs <author>Bob</author> ✗ Different

Result: One difference detected (author name changed)

3.3. No Move Detection

If elements are reordered, the DOM algorithm shows this as deletions and additions:

Example 2. Reordered elements shown as changes
<!-- Document 1 -->
<book>
  <title>Canon</title>
  <author>Alice</author>
</book>

<!-- Document 2 -->
<book>
  <author>Alice</author>
  <title>Canon</title>
</book>

DOM algorithm output:

- <title>Canon</title>
+ <author>Alice</author>
  <author>Alice</author>
+ <title>Canon</title>

The same content appears as both deleted and added because positions changed.

4. Configuration

4.1. Basic Usage

Ruby API:

# DOM algorithm is the default
Canon::Comparison.equivalent?(doc1, doc2)

# Or explicitly specify
Canon::Comparison.equivalent?(doc1, doc2,
  diff_algorithm: :dom
)

CLI:

# DOM algorithm is the default
canon diff file1.xml file2.xml

# Or explicitly specify
canon diff file1.xml file2.xml --diff-algorithm dom

4.2. With Match Options

DOM algorithm respects all match dimensions:

Canon::Comparison.equivalent?(doc1, doc2,
  diff_algorithm: :dom,
  match: {
    text_content: :normalize,
    structural_whitespace: :ignore,
    attribute_order: :ignore
  }
)

See Algorithm-Specific Behavior for how DOM interprets match options.

4.3. With Diff Formatting

The DOM algorithm works with both diff modes:

# Line-based output (natural fit for DOM)
Canon::Comparison.equivalent?(doc1, doc2,
  diff_algorithm: :dom,
  diff_mode: :by_line,
  verbose: true
)

# Tree-based output (also works)
Canon::Comparison.equivalent?(doc1, doc2,
  diff_algorithm: :dom,
  diff_mode: :by_object,
  verbose: true
)

5. Output Format

5.1. Line-Based Output (Default)

The DOM algorithm naturally produces line-based differences:

Example 3. Line-based diff example
1 | <book>
2-|   <title>Old Title</title>
 +|   <title>New Title</title>
3 |   <author>Alice</author>
4 | </book>
  • Lines with - were removed

  • Lines with + were added

  • Unchanged lines provide context

5.2. Tree-Based Output

The DOM algorithm can also produce tree-based output:

Example 4. Tree-based diff example
book
  title
    - Old Title
    + New Title
  author
    = Alice
  • Shows hierarchical structure

  • Still position-based (no move operations)

6. Advantages

6.1. Fast Performance

  • Linear time complexity - O(n) where n is document size

  • Low memory usage - Processes line-by-line

  • Scales well - Handles documents > 100KB easily

Table 1. Performance comparison
Document Size DOM Time Notes

1 KB

~1 ms

Very fast

10 KB

~10 ms

Still fast

100 KB

~100 ms

Acceptable

1 MB

~1 s

Good scaling

6.2. Stable and Predictable

  • Production-ready - Well-tested, no surprises

  • Consistent behavior - Same results every time

  • Widely used - Default for all Canon comparisons

6.3. Works Everywhere

  • All formats - XML, HTML, JSON, YAML

  • All diff modes - by_line and by_object

  • All match profiles - Compatible with all configurations

7. Limitations

7.1. No Move Detection

The DOM algorithm cannot detect when content moves to a different position:

Limitation: Reordered content shows as DELETE + INSERT pairs

Workaround: Use Semantic Algorithm for move detection

7.2. Position-Dependent

The algorithm assumes similar structure between documents:

Limitation: Heavily restructured documents produce noisy diffs

Workaround: Use Semantic Algorithm for restructured documents

8. Common Use Cases

8.1. Use Case 1: Testing XML Generation

# Fast, reliable test assertion
RSpec.describe "XML generation" do
  it "generates correct XML" do
    actual = MyGenerator.generate
    expected = File.read("expected.xml")

    expect(actual).to be_xml_equivalent_to(expected)
      .with_profile(:spec_friendly)
    # Uses DOM algorithm by default
  end
end

8.2. Use Case 2: Code Review Diff

# Traditional diff for reviewing changes
canon diff old.xml new.xml \
  --diff-algorithm dom \
  --diff-mode by_line \
  --verbose \
  --use-color

8.3. Use Case 3: CI/CD Validation

# Fast validation in CI pipeline
result = Canon::Comparison.equivalent?(expected, actual,
  diff_algorithm: :dom,  # Fast
  match_profile: :spec_friendly
)

unless result
  puts "Validation failed!"
  exit 1
end

9. Best Practices

9.1. Use for Most Comparisons

The DOM algorithm is the right choice for 90% of use cases. Only switch to Semantic when you specifically need move detection.

9.2. Combine with Match Profiles

Use appropriate match profiles for your use case:

# For tests
Canon::Comparison.equivalent?(doc1, doc2,
  match_profile: :spec_friendly
)

# For exact validation
Canon::Comparison.equivalent?(doc1, doc2,
  match_profile: :strict
)

# For rendered HTML
Canon::Comparison.equivalent?(html1, html2,
  match_profile: :rendered
)

9.3. Use Appropriate Diff Mode

  • by_line - Best for code review, traditional diffs

  • by_object - Best for structured view, test output

10. See Also