The fastest Diff and patch library in Swift. Includes UICollectionView/UITableView utils.
Alternatives To Diff.swift
Project NameStarsDownloadsRepos Using ThisPackages Using ThisMost Recent CommitTotal ReleasesLatest ReleaseOpen IssuesLicenseLanguage
4 days ago57July 27, 2023143mitRust
a structural diff that understands syntax 🟥🟩
194 months ago21May 07, 202127apache-2.0Swift
💻 A fast and flexible O(n) difference algorithm framework for Swift collection.
Textdistance3,1721442a year ago25September 20, 20229mitPython
📐 Compute distance between sequences. 30+ algorithms, pure python implementation, common interface, optional external libs usage.
263 years ago12October 22, 201818mitSwift
Swift Diff
205 years ago7September 30, 20178mitSwift
The fastest Diff and patch library in Swift. Includes UICollectionView/UITableView utils.
Nanomorph710225652 years ago33February 18, 202117mitJavaScript
🚅 - Hyper fast diffing algorithm for real DOM nodes
2 years ago4June 08, 202113apache-2.0Swift
💾 A library for backporting UITableView/UICollectionViewDiffableDataSource.
6 months ago22August 25, 202211epl-1.0Clojure
A library to diff and patch Clojure/ClojureScript data structures
Vim Diff Enhanced332
3 years agoVim script
Better Diff options for Vim
Diff Lcs263214,363323a year ago16December 23, 20215otherRuby
Generate difference sets between Ruby sequences.
Alternatives To Diff.swift
Select To Compare

Alternative Project Comparisons

This project has moved here

Since I don't use Diff.swift anymore Tony Arnold kindly offered to take over maintenance of this project in his own fork. Don't use Diff.swift, this repo is only kept as an archive.

Build Status codecov


This library generates differences between any two Collections (and Strings). It uses a fast algorithm (O((N+M)*D)).


Documentation is available here


  • Diff.swift supports three types of operations:
    • Insertions
    • Deletions
    • Moves (use ExtendedDiff)
  • Arbitrary sorting of the Patch
  • Utilities for UITableView and UICollectionView (if that's just what you want, skip to examples)
  • ⚡️ fast
  • Diffing collections containing collections (use NestedDiff)

Why would I need it?

There's more to diffs than performing UITableView animations easily.

Wherever you have code which propagates added/removed/moved callbacks from your model to the UI it's good to consider using a diffing library instead. What you get is clear separation and more declarative approach. The model just performs state transition and the UI code performs appropriate UI actions based on the diff output.

Diff vs Patch (Sorting)

Let's consider a simple example of a patch to transform string "a" into "b".

  1. Delete item at index 0 (we get "")
  2. Insert b at index 0 (we get "b")

If we want to perform these operations in different order, simple reordering of the steps doesn't work.

  1. Insert b at index 0 (we get "ba")
  2. Delete item at index 0 (we get "a")

... ooooops

We need to shift insertions and deletions so that we get this:

  1. Insert b at index 1 (we get "ab"
  2. Delete item at index 0 (we get "b")


In order to mitigate this issue there are two types of output:

  • Diff
    • A sequence of deletions, insertions, and moves (if using ExtendedDiff) where deletions point to locations of an item to be deleted in the source and insertions point to the items in the output. Diff.swift produces just one Diff.
  • Patch
    • An ordered sequence of steps to be applied to obtain the second sequence from the first one. It is based on a Diff but can be arbitrarly sorted.

Sorting in practice

In practice it means that a diff to transform string "1234" to "1" is "D(1)D(2)D(3)" the default patch is "D(1)D(1)D(1)". However, if we decide to sort it so that deletions and bigger indices happen first we get this patch: "D(3)D(2)D(1)".

How to use


// It will automatically animate deletions, insertions, and moves
            oldData: old,
            newData: new)

    oldData: old,
    newData: new,
    completion: {_ in}) 

// Works with sections, too

    oldData: old,
    newData: new

    oldData: old,
    newData: new

See examples for a working example.

Using Patch and Diff

When you want to get steps to transform one sequence into another (e.g. you want to animate UI according to the changes in the model)

let from: T
let to: T

// only insertions and deletions
// Returns [Patch<T.Iterator.Element>]
let patch = patch(
                from: from,
                to: to

// Patch + moves
// Returns [ExtendedPatch<T.Iterator.Element>]
let patch = extendedPatch(
                from: from,
                to: to

When you need additional control over ordering

let insertionsFirst = { element1, element2 -> Bool in 
    switch (element1, element2) {
    case (.insert(let at1), .insert(let at2)):
        return at1 < at2
    case (.insert, .delete):
        return true
    case (.delete, .insert):
        return false
    case (.delete(let at1), .delete(let at2)):
        return at1 < at2
    default: fatalError() // unreachable

// Results in a [Patch] with insertions preceeding deletions
let patch = patch(
                from: from,
                to: to,
                sort: insertionsFirst

More advanced - you want to calculate diff first and generate patch. In certain cases it's a good performance improvement. Generating a sorted patch takes O(D^2) time. The default order takes O(D) to generate. D is the length of a diff.

// Generate diff first
let diff = from.diff(to)
let patch = diff.patch(from: from, to: to)

Performance notes

This library is fast. Most other libraries use a simple O(n*m) algorithm which allocates a 2 dimensional array and goes through all elements. It takes a lot of memory. In the benchmark it is an order of magnitude difference.

Source code is available here. The result of a measurement is mean diff time in seconds over 10 runs on an iPhone 6.

             | Diff.swift | Dwifft 
     same    |   0.0213   | 52.3642
     created |   0.0188   | 0.0033
     deleted |   0.0184   | 0.0050
     diff    |   0.1320   | 63.4084

This algorithm works great for collections with small diffs. I mean, even for big diffs, it's still better than the simple algorithm. However, if you need good performance and you have big differences between the inputs consider another diffing algorithm. Look at Hunt & Szymanski's and/or Hirschberg's work.


Carthage (preferred)

// Cartfile
github "wokalski/Diff.swift"


// podfile
pod 'Diff'

Swift 4.0

There's a swift-4.0 branch with the patches by @tonyarnold.

Get in touch

If you have any questions, you can find me on Twitter.


If you want to learn how it works Graph.playground is a good place to start.

Popular Diff Projects
Popular Algorithms Projects
Popular Text Processing Categories

Get A Weekly Email With Trending Projects For These Categories
No Spam. Unsubscribe easily at any time.