<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.14.168/lodash.min.js'></script>
var obj1 = {
"latitude": 56.282,
"longitude": 43.995,
"_heatmapValue": 38680
}
var obj2 = {
"latitude": 56.27,
"longitude": 43.992,
"_heatmapValue": 25439
}
var result = null;
result = _.isEqual(obj1, obj2);
result = JSON.stringify(obj1) === JSON.stringify(obj2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash isEqual | |
stringify |
Test name | Executions per second |
---|---|
lodash isEqual | 1538785.4 Ops/sec |
stringify | 599518.8 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition:
The benchmark is comparing two approaches to check if two objects are equal:
lodash isEqual
JSON.stringify
(objects)Options Compared:
JSON.stringify
and then compares these strings for equality. This method is more invasive as it serializes the entire object, including all its properties, not just the ones that are different.Pros and Cons:
Library:
isEqual
function is one of its most commonly used functions.Special JS Feature/Syntax:
None mentioned in this specific benchmark.
Other Alternatives:
===
operator to compare objects directly (i.e., without serialization). However, this would require checking all properties manually, which can lead to complex and hard-to-maintain code.fast-json-stamp
or json-stringify-safe
, which are designed for efficient stringification of JSON-like objects.In summary, the benchmark is comparing two approaches to check if two objects are equal: one uses the Lodash isEqual
function and the other uses serialization with JSON.stringify
. The choice between these approaches depends on factors such as performance, simplicity, and the specific requirements of your application.