@cocalc/diff-match-patch

中文文档
1.1.1发布于 2026/10/24Apache-2.0TypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2772

Diff-Match-Patch in typescript with full budget and surrogate pairs support

原始使用文档

Diff-Match-Patch in Typescript with full budget and surrogate pairs support

Javascript diff-match-patch, made suitable for use in production:

  • Modern typescript with unit tests, thanks to diff-match-patch-typescript.
  • Full unicode support, thanks to PR 80
  • I added full diffTimeout support, which is only partially implemented elsewhere, and is critical for production use (since otherwise your server or browser can be easily stuck at 100% for minutes). See big.test.ts.
  • A deterministic work budget (diffWorkLimit, or a DiffWorkBudget passed in) in place of the clock. Off by default; without it, results are byte-identical to 1.0.1. See work-budget.test.ts. With it:
    • The contract: diffWorkLimit bounds the abstract, charged algorithm steps of diff_main and the cleanups (and so of patch_make's diff and patch_apply's fuzzy diffs): at most diffWorkLimit + BUDGET_STEP_UNITS × (n + 2) units, where n is the length of the call's texts (for a cleanup, of its diffs) and BUDGET_STEP_UNITS is 24. The units track the characters the algorithm examines and copies and the diffs it moves. Engine-level work is not metered: string concatenation and flattening, ===, the semantic score's regular expressions, array reads and writes, and the line encoding's hashing. Its cost may vary by engine, so the units bound the algorithm's steps, not total CPU.
    • How: every step is either paid for ahead with a bound on its work (and skipped, leaving the trivial diff, if the budget can't cover it), or runs only while the budget isn't spent and is charged as it runs. So the charged units are the limit plus at most one step. One budget is shared by everything a call does, and the clock is never read.
    • Not covered: patch_make's assembly and its context search (patch_addContext_, O(N) per patch) are outside the budget, and so is patch_apply's matching (match_main). The budget bounds the diff, not all of patch_make.
    • Speed: on V8 (Node) on an AMD EPYC 7B13 core, roughly 135,000 to 180,000 units take a millisecond, so diffWorkLimit: 25_000_000 is about diffTimeout: 0.2 there. That figure is calibrated and measured there, not guaranteed, and not part of the contract.
  • Modern api for creating dmp object (still backward compatible).

Installation

pnpm i @cocalc/diff-match-patch
import { DiffMatchPatch } from "@cocalc/diff-match-patch";
const dmp = new DiffMatchPatch({ diffTimeout: 0.5 });
const patch = dmp.patch_make("ello", "hello world");
console.log(JSON.stringify(patch));
// [{"diffs":[[1,"h"],[0,"ello"],[1," world"]],"start1":0,"start2":0,"length1":4,"length2":11}]
dmp.patch_apply(patch, "ello");
// [ 'hello world', [ true ] ]

Documentation

Official Repository.

版本与发布记录

@cocalc/diff-match-patch 共发布 4 个版本,首次发布于 2025/10/08;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.1latest2026/10/2417 天前
1.1.02026/10/2120 天前
1.0.12025/10/081.1 年前
1.0.02025/10/081.1 年前

关于 @cocalc/diff-match-patch 的常见问题

@cocalc/diff-match-patch 是什么?

Diff-Match-Patch in typescript with full budget and surrogate pairs support

如何安装 @cocalc/diff-match-patch?

在项目目录运行 npm install @cocalc/diff-match-patch,或使用 pnpm add / yarn add / bun add。

@cocalc/diff-match-patch 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@cocalc/diff-match-patch 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@cocalc/diff-match-patch 可以商用吗?

@cocalc/diff-match-patch 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@cocalc/diff-match-patch 的下载量有多少?

根据 npm 官方下载统计,@cocalc/diff-match-patch 在 2026/10/10 – 2026/11/08 这 30 天内被下载 2,772 次,日均约 92 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cocalc/diff-match-patch 还在维护吗?

最新版本 1.1.1 发布于 2026/10/24(17 天前);至今共发布 4 个版本,其中过去 12 个月发布了 2 个;首次发布于 2025/10/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。