Skip to content

Commit

Permalink
add compate with diff-match-patch
Browse files Browse the repository at this point in the history
  • Loading branch information
cudr committed Jul 25, 2019
1 parent 8cc682e commit 13fd876
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
18 changes: 17 additions & 1 deletion benchmark/jsDiffCompare.test.ts → benchmark/benchmark.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { diffWordsWithSpace, diffChars } from "diff";
import { diff_match_patch } from 'diff-match-patch'
import stringDiffToOps from "../src/compare/stringDiffToOps";
import { applyOps } from "../src/operations";
import { genSentences, randomizeText } from "./index";

describe("benchmark jsdiff compare", () => {
describe("benchmark diff compare", () => {
const origin = genSentences(50);
const modifyed = randomizeText(origin);

let dmp: any = new diff_match_patch()

console.time("csto stringDiffToOps");
const operations = stringDiffToOps(origin, modifyed);
console.timeEnd("csto stringDiffToOps");
Expand All @@ -15,6 +18,15 @@ describe("benchmark jsdiff compare", () => {
const diff = diffWordsWithSpace(origin, modifyed);
console.timeEnd("jsdiff diffWordsWithSpace");

console.time('diff-match-patch diff_main')
const dmp_diff = dmp.diff_main(origin, modifyed);
console.timeEnd('diff-match-patch diff_main')

console.time('diff-match-patch patch_make')
const dmp_patch = dmp.patch_make(origin, modifyed);
console.timeEnd('diff-match-patch patch_make')


const result = diff.reduce(
(acc: any, el) => (el.removed ? acc : acc + el.value),
""
Expand All @@ -27,4 +39,8 @@ describe("benchmark jsdiff compare", () => {
it("string jsdiff equals", () => {
expect(result).toBe(modifyed);
});

it("string diff-match-patch equals", () => {
expect(dmp.patch_apply(dmp_patch, origin)[0]).toBe(modifyed);
});
});
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@types/diff": "^4.0.2",
"@types/faker": "^4.1.5",
"diff": "^4.0.1",
"diff-match-patch": "^1.0.4",
"faker": "^4.1.0"
},
"jest": {
Expand All @@ -72,5 +73,8 @@
"yarn run format",
"git add"
]
},
"dependencies": {
"@types/diff-match-patch": "^1.0.32"
}
}

0 comments on commit 13fd876

Please sign in to comment.