<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fast-deep-equal@3.1.3/index.min.js"></script>
// 1 level deep
window.foo1 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2 } } };
window.bar1 = { a: 1, b: 3, c: { a: 1, b: 2, c: { a: 1, b: 2 } } };
// 2 levels deep
window.foo2 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2 } } };
window.bar2 = { a: 1, b: 2, c: { a: 1, b: 3, c: { a: 1, b: 2 } } };
// 3 levels deep
window.foo3 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 2 } } };
window.bar3 = { a: 1, b: 2, c: { a: 1, b: 2, c: { a: 1, b: 4 } } };
_.isEqual(window.foo1, window.bar1)
equal(window.foo1, window.bar1);
JSON.stringify(window.foo1) === JSON.stringify(window.bar1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash | |
fast-deep-equal | |
JSON |
Test name | Executions per second |
---|---|
lodash | 753009.1 Ops/sec |
fast-deep-equal | 0.0 Ops/sec |
JSON | 467531.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
Benchmark Overview
The provided JSON represents a benchmarking test for three different approaches: Lodash's isEqual
function, Fast-Deep-Equal's equal
function, and the built-in JSON
equality check. The test creates two sets of objects at varying levels of depth (1, 2, and 3) and compares them using each approach.
Options Compared
isEqual
function: This function checks for deep object equality by recursively traversing the objects' properties.equal
function: This function is a specialized implementation of deep object equality, optimized for performance.JSON
equality check: This method uses JSON stringification to compare two values and checks if they are equal.Pros and Cons
isEqual
function:equal
function:JSON
equality check:Library and Purpose
isEqual
function is used to compare two objects.Special JS Feature or Syntax
None mentioned in the provided code, but it's worth noting that JavaScript has several features and syntaxes that can affect performance, such as:
let
and const
declarations (vs. traditional var
)() => {}
) vs. traditional function expressionsHowever, these are not relevant to the specific benchmark being discussed.
Other Alternatives
If you need a custom implementation or alternative approach for deep object equality, consider using:
for
loops or libraries like lodash's
each
method to compare properties.Keep in mind that these alternatives may have different performance characteristics and trade-offs compared to the specialized implementations provided by Fast-Deep-Equal and Lodash.