Skip to content

Massive performance degradation when truncating large file (20,000x slower than Google's diff-match-patch package) #239

@josephrocca

Description

@josephrocca

Hi there, I've been having some performance problems with createPatch and have managed to create a minimal example that replicates the problem. The patch creation takes about 20,000 times longer than it should. It seems to occur when I'm doing large truncations. Here's the code:

const {performance} = require("perf_hooks");
const JsDiff = require('diff');
const diff_match_patch = require("diff-match-patch");
const dmp = new diff_match_patch();

function makePatch(oldText, newText) {
  let t;

  t = performance.now();
  let textPatch1 = JsDiff.createPatch("foo.txt", oldText+"\n", newText+"\n", "", "");
  console.log("JsDiff (ms):", performance.now()-t);

  t = performance.now();
  let textPatch2 = dmp.patch_make(oldText+"\n", newText+"\n");
  console.log("diff_patch_match (ms):", performance.now()-t);
}

// make a string of 25k lines of random numbers:
text1 = new Array(25000).fill(0).map(n => new Array(20).fill(1).map(n => Math.floor(Math.random()*10)).join("")).join("\n");
// grab the first 10,000 characters:
text2 = text1.slice(0, 10000);
// make the patch:
makePatch(text1, text2);

Here's the output I get for that:

JsDiff (ms): 133238.9396559
diff_patch_match (ms): 6.825272083282471

So unless I'm doing something wrong here, there's a huge performance disparity between this package and the diff-patch-match package for patch creation in certain situations. diff-patch-match is normally about 2x-10x faster depending on the size of the strings, but here its a lot faster... so something's obviously going wrong here.

This is what I'm seeing in the profiler:

image

Any idea what's happening here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions