<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
// 1 level deep
window.foo1 = [
{id: "1035257027", person_id: "1043549930", group_id: "551225445", avatar_url: "http://people.pco.test/static/no_photo_thumbnail_gray.png", email_address: "pico@pco.bz"},
{id: "1035257027", person_id: "1043549930", group_id: "551225445", avatar_url: "http://people.pco.test/static/no_photo_thumbnail_gray.png", email_address: "pico@pco.bz"},
];
window.bar1 = [
{id: "1035257027", person_id: "1043549930", group_id: "551225445", avatar_url: "http://people.pco.test/static/no_photo_thumbnail_gray.png", email_address: "pico@pco.bz"},
{id: "1035257025", person_id: "1043549930", group_id: "551225445", avatar_url: "http://people.pco.test/static/no_photo_thumbnail_gray.png", email_address: "pico@pco.bz"},
];
_.isEqual(window.foo1, window.bar1)
JSON.stringify(window.foo1) === JSON.stringify(window.bar1);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.isEqual Level 1 | |
JSON.stringify Level 1 |
Test name | Executions per second |
---|---|
_.isEqual Level 1 | 1261262.5 Ops/sec |
JSON.stringify Level 1 | 1739225.6 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
The provided benchmark definition and individual test cases are used to measure the performance of the isEqual
function from the Lodash library. Here's what's being tested:
isEqual
function is a utility function that checks if two objects or arrays are deeply equal. It's used to compare complex data structures, such as nested objects and arrays.isEqual
function (_.isEqual(window.foo1, window.bar1)
).JSON.stringify()
(JSON.stringify(window.foo1) === JSON.stringify(window.bar1)
).Pros and Cons:
JSON.stringify()
implementation.Library:
The Lodash library is a popular utility library for JavaScript that provides a wide range of functions for tasks such as array manipulation, object transformation, and more. In this case, the isEqual
function is used to compare complex data structures.
Special JS feature or syntax:
None mentioned in the provided benchmark definition. However, note that the use of window.foo1
and window.bar1
suggests a global variable context, which may not be representative of typical web development scenarios where variables are scoped to functions or modules.
Other alternatives:
If you want to compare two objects or arrays without using Lodash, you can consider implementing your own equality check function. Here's an example:
function isEqual(obj1, obj2) {
if (typeof obj1 !== 'object' || typeof obj2 !== 'object') return false;
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) return false;
for (const key of keys1) {
if (!isEqual(obj1[key], obj2[key])) return false;
}
return true;
}
Alternatively, you can use a library like fast-json-stable-stringify
or json-stable-stringify
to compare JSON strings.
Keep in mind that the choice of equality check function depends on your specific use case and performance requirements.