<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
window.foo1 = {a: 1, b: {a: 1, b:2, c: [1, 2, 3, [1, 3], {a: 1}]}};
window.foo2 = {a: 1, b: {a: 1, b:2, c: [1, 2, 3, [1, 3], {a: 1}]}, x: ''};
window.foo3 = {a: 0, b: {a: 1, b:2, c: [1, 2, 3, [1, 3], {a: 1}]}};
window.foo4 = {a: 1, b: {a: 1, b:2, c: [2, 3, [1, 3], {a: 1}]}};
window.bar = {a: 1, b: {a: 1, b:2, c: [1, 2, 3, [1, 3], {a: 1}]}};
_.isEqual(window.foo1, window.bar)
JSON.stringify(window.foo1) === JSON.stringify(window.bar);
_.isEqual(window.foo2, window.bar)
_.isEqual(window.foo3, window.bar)
_.isEqual(window.foo4, window.bar)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.isEqual full | |
JSON.stringify | |
_.isEqual parted 1 | |
_.isEqual parted 2 | |
_.isEqual parted 3 |
Test name | Executions per second |
---|---|
_.isEqual full | 289631.4 Ops/sec |
JSON.stringify | 531783.5 Ops/sec |
_.isEqual parted 1 | 1281525.9 Ops/sec |
_.isEqual parted 2 | 921811.4 Ops/sec |
_.isEqual parted 3 | 493310.8 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
The provided JSON represents a benchmark test created on MeasureThat.net, which compares the performance of Lodash.isEqual
with a simple string comparison using JSON.stringify
. Here's what's being tested:
Test cases:
_isEqual full
: Tests if _.isEqual
returns true for both objects, foo1
and bar
._isEqual parted 1
: Tests if _.isEqual
returns true for the first object part (only a
property) of foo1
and bar
. Note that this is a partial comparison._isEqual parted 2
: Tests if _.isEqual
returns true for the second object part (only b
property) of foo1
and bar
._isEqual parted 3
: Tests if _.isEqual
returns true for a modified version of foo1
with an extra property (x
) that's empty.JSON.stringify
: Tests if a simple string comparison using JSON.stringify
can determine whether two objects are equal.Library:
The Lodash.isEqual
function is part of the Lodash library, which provides various utility functions for functional programming, data manipulation, and more. In this case, isEqual
checks whether two values are strictly equal.
Pros and Cons:
JSON.stringify
: Pros:Other considerations:
foo1
and bar
objects are carefully crafted to ensure that the _isEqual full
, _isEqual parted 1
, _isEqual parted 2
, and _isEqual parted 3
test cases cover different scenarios.JSON.stringify
test case is straightforward but might not be as efficient for large objects.Alternatives:
For object comparisons, you can use other libraries like:
DeepEqual
from deep-equal
Object.is()
(if supported by your target browsers)For string comparison, you can also consider:
fast-json-stamp
for efficient string comparison