<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 | 2177127.8 Ops/sec |
stringify | 2870652.5 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and its pros and cons.
Benchmark Definition
The benchmark defines two test cases: lodash isEqual
and stringify
. The script preparation code creates two identical arrays of objects (obj1
and obj2
) with different values. The purpose of this setup is to compare the performance of these two approaches in identifying whether two objects are equal.
Lodash isEqual
Lodash's isEqual
function is used to check if two objects have the same key-value pairs, regardless of data type. In this test case, Lodash's implementation checks for deep equality, meaning it compares not only the values but also the object structure and reference types.
Pros:
Cons:
JSON.stringify
The stringify
approach converts both objects to strings using JSON.stringify
. This method returns a string representation of the object, including all key-value pairs and nested structures.
Pros:
Cons:
Library
The benchmark uses the popular JavaScript library Lodash. Its purpose is to provide a set of utility functions, including isEqual
, that can simplify code and improve readability. In this test case, it's used to compare two objects with identical data structures but different values.
Special JS Features/Syntax
There are no special JavaScript features or syntax being tested in this benchmark. The focus is on comparing the performance of two existing approaches: Lodash's isEqual
function and the built-in stringify
method.
Other Alternatives
If you need to compare the performance of object equality checks, you can consider using:
===
, ==
, or even Object.is()
(available in modern browsers) to check for equality.isEqual
function.Keep in mind that the choice of approach depends on your specific use case and performance requirements.