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
-
Parse Documents - Convert both documents to DOM trees
-
Compare Positions - Compare elements at each position in the tree
-
Apply Match Options - Use configured dimensions for comparison
-
Generate Differences - Create line-based diff output
3.2. Position-Based Matching
The DOM algorithm compares elements by their position in the document:
<!-- 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:
<!-- 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:
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
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
| 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 |
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
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
)
10. See Also
-
Algorithms Overview - Comparison of DOM vs Semantic
-
Semantic Algorithm - Alternative algorithm
-
Algorithm-Specific Behavior - How DOM interprets options
-
Algorithm-Specific Output - Output format details
-
Choosing Configuration - Complete decision guide
-
DOM Diff - Implementation details