Use optimized diff methods for JSON and YAML
mainWhen comparing structured data like JSON or YAML, use the dedicated DiffMethod constants. These methods are significantly faster (up to 100x) for large files because they parse the data structure and perform a structural comparison instead of a character-by-character text diff.
- JSON: Use
DiffMethod.JSON. You can pass JavaScript objects directly tooldValueandnewValue. - YAML: Use
DiffMethod.YAML. Pass YAML strings tooldValueandnewValue.
import React from 'react';
import ReactDiffViewer, { DiffMethod } from 'react-diff-viewer-continued';
// JSON Example
function JsonDiff() {
const oldJson = { name: "Original", items: [1, 2, 3] };
const newJson = { name: "Updated", items: [1, 2, 3, 4] };
return (
<ReactDiffViewer
oldValue={oldJson}
newValue={newJson}
compareMethod={DiffMethod.JSON}
splitView={true}
/>
);
}
// YAML Example
function YamlDiff() {
const oldYaml = `name: Original\nitems:\n - 1`;
const newYaml = `name: Updated\nitems:\n - 1\n - 2`;
return (
<ReactDiffViewer
oldValue={oldYaml}
newValue={newYaml}
compareMethod={DiffMethod.YAML}
splitView={true}
/>
);
}