<script type='text/javascript' src="https://cdn.jsdelivr.net/npm/jsondiffpatch/dist/jsondiffpatch.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/deep-diff@1/dist/deep-diff.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fast-json-patch/dist/fast-json-patch.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/rfc6902/dist/rfc6902.min.js"></script>
obj1= {
name: "Argentina",
cities: [
{
name: 'Buenos Aires',
population: 13028000,
},
{
name: 'Cordoba',
population: 1430023,
},
{
name: 'Rosario',
population: 1136286,
},
{
name: 'Mendoza',
population: 901126,
},
{
name: 'San Miguel de Tucuman',
population: 800000,
}
]
};
obj2= {
name: "Argentina",
cities: [
{
name: 'Cordoba',
population: 1430023,
},
{
name: 'Mendoza',
population: 901126,
},
{
name: 'San Miguel de Tucuman',
population: 550000,
}
]
};
var diff1 = jsondiffpatch.diff(obj1, obj2);
/*var objnew = jsonpatch.deepClone(obj1);
// jsondiffpatch.unpatch(objnew, diff1);*/
var diff3 = jsonpatch.compare(obj1, obj2);
/*var objnew = jsonpatch.deepClone(obj1);
jsonpatch.applyPatch(objnew, diff3, false, true);*/
var diff4 = rfc6902.createPatch(obj1, obj2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
jsondiffpatch | |
FAST JSON-patch | |
rfc6902 |
Test name | Executions per second |
---|---|
jsondiffpatch | 321620.4 Ops/sec |
FAST JSON-patch | 856301.9 Ops/sec |
rfc6902 | 21272.6 Ops/sec |
The benchmark you provided tests the performance of three different JavaScript libraries used for computing differences (or "diffs") between two JSON objects. The libraries being compared are:
jsondiffpatch: This library provides a way to compute the difference between two JSON objects and also supports "patching," which allows changes to be applied to a JSON object based on a computed diff. It is generally more focused on giving a human-readable representation of the changes, supporting advanced features like tracking added, removed, or altered properties.
fast-json-patch: This library is focused on efficiently applying JSON Patch operations. JSON Patch is a standardized format (RFC 6902) for describing changes to a JSON document. The key advantage is its speed; hence the use of “fast” in the name. It uses an algorithm optimized for reducing the amount of work needed to create and apply changes.
rfc6902: This library provides a direct implementation of the JSON Patch specification (RFC 6902). It allows the creation and application of patches following this standard, suitable for cases where a strict adherence to the RFC is crucial.
The benchmark runs three testing cases, each of which uses one of the libraries to determine the performance of the diff operation between two JSON objects defined as obj1
and obj2
.
jsondiffpatch: Computes the difference between obj1
and obj2
and is typically slower than the others due to its feature-rich implementation and focus on providing more extensive detalization of changes.
fast-json-patch: Compared to the other two, it shines in performance, yielding the highest "Executions Per Second." It uses an optimized algorithm designed specifically to handle JSON patches quickly, which is a big time-saver in scenarios that involve frequent updates to JSON structures.
rfc6902: The performance here is noted to be considerably lower than the first two libraries. Its operation is straightforward and compliance-focused, making it less about speed and more about conforming to specifications.
Based on the latest benchmark results:
The benchmarks clearly indicate that fast-json-patch is superior in terms of execution speed when only the diff operation is considered. However, one must consider additional trade-offs, such as the complexity of operations and features offered by each library when choosing one for use in a project.
jsondiffpatch
fast-json-patch
rfc6902
For developers, other alternatives may include:
In summary, the choice of which library to use should hinge on the specific needs of the project, including performance requirements, adherence to standards, and the complexity of the data being managed.