<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)
window.foo1 === window.bar1;
window.foo1 === window.foo1;
_.isEqual(window.foo1, window.foo1)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.isEqual different objects | |
=== different objects | |
=== same object | |
_.isEqual same object |
Test name | Executions per second |
---|---|
_.isEqual different objects | 1051150.2 Ops/sec |
=== different objects | 6566962.5 Ops/sec |
=== same object | 6939338.5 Ops/sec |
_.isEqual same object | 4805015.5 Ops/sec |
Let's dive into the Benchmark Definition and explain what is tested on the provided JSON.
The benchmark compares three ways to compare arrays of objects for equality:
isEqual
function (_.isEqual(window.foo1, window.bar1)
).===
) with two different arrays (window.foo1 === window.bar1;
and window.foo1 === window.foo1;
).window.foo1 === window.bar1;
).Let's break down each option:
Option 1: Lodash's isEqual
function
Lodash's isEqual
function is a utility function that checks if two values are equal, taking into account various types such as arrays, objects, numbers, and more. In this benchmark, it's used to compare the foo1
and bar1
arrays.
Pros:
Cons:
Option 2: Strict equality operator (===
)
The strict equality operator compares the values of two expressions using their string representations. In this benchmark, it's used to compare the foo1
and bar1
arrays directly.
Pros:
Cons:
foo1
and bar1
have different lengths, ===
will return false.Option 3: Simple comparison of object references (window.foo1 === window.bar1;
)
This option is not recommended as it's prone to errors. In JavaScript, objects are passed by reference, so comparing two variables using ===
only checks if they point to the same memory location. If this condition is met, it means both variables refer to the same object in memory.
Pros: None (this option has significant limitations).
Cons:
Now, let's talk about special JavaScript features used in this benchmark. There is no explicit mention of any specific features like let
or const
, but the use of window
variables suggests that the test environment uses a global scope with variable declarations.
Other alternatives to compare arrays:
===
.===
but uses a more nuanced comparison that handles edge cases better.Keep in mind that different browsers or JavaScript engines might have slightly different performance characteristics for these alternatives.