Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
compare arrays
(version: 0)
lodash vs json stringify and other methods
Comparing performance of:
_.isEqual vs JSON.stringify vs equals vs equalsIgnoreOrder
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
window.foo = ['cat', 'dog', 'bird']; window.bar = ['cat', 'dog', 'bird']; window.equals = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]); window.equalsIgnoreOrder = (a, b) => { if (a.length !== b.length) return false; const uniqueValues = new Set([...a, ...b]); for (const v of uniqueValues) { const aCount = a.filter(e => e === v).length; const bCount = b.filter(e => e === v).length; if (aCount !== bCount) return false; } return true; }
Tests:
_.isEqual
_.isEqual(window.foo, window.bar)
JSON.stringify
JSON.stringify(window.foo) === JSON.stringify(window.bar);
equals
window.equals(window.foo, window.bar)
equalsIgnoreOrder
window.equalsIgnoreOrder(window.foo, window.bar)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
_.isEqual
JSON.stringify
equals
equalsIgnoreOrder
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.isEqual
15618695.0 Ops/sec
JSON.stringify
8565059.0 Ops/sec
equals
116523576.0 Ops/sec
equalsIgnoreOrder
7431307.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explore what's being tested. **Benchmark Definition** The benchmark compares three different methods to compare two arrays: `lodash`'s `_isEqual` function, `JSON.stringify` with equality check using `===`, and a custom `equals` function implemented by the test user. The third method is an implementation of the "ignore order" approach for comparing arrays, which ignores the original order of elements when checking for equality. **Options Compared** 1. **_.isEqual (Lodash)**: This function checks if two objects are equal, ignoring the order of properties and only considering enumerable own properties. 2. **JSON.stringify + ===**: This method converts both arrays to strings using `JSON.stringify` and then compares these strings using the `===` operator. Since this approach converts the arrays to strings, it doesn't take into account any potential differences in element order or data type. 3. **equals (Custom)**: This function checks if two arrays have the same length and, for each element, checks if both elements are equal. If the lengths differ, it returns `false`. If an element is missing from one array compared to the other, it also returns `false`. **Pros and Cons** * **_.isEqual (Lodash)**: + Pros: Efficiently handles complex object structures and ignores property order. + Cons: May have a performance overhead due to its complexity, especially for large objects or arrays. * **JSON.stringify + ===**: + Pros: Simple to implement and has a low overhead. It's also easy to understand and debug. + Cons: Converts the arrays to strings, which can lead to performance issues with large datasets. It doesn't take into account element order differences. * **equals (Custom)**: + Pros: Ignores element order and data type when comparing arrays. This approach is straightforward and easy to implement. + Cons: Can be slower than the other approaches, especially for large arrays, since it needs to check each element individually. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like array manipulation, object creation, and more. The `_isEqual` function in this benchmark is part of Lodash's `MoreUtils` module, which provides functions for comparing objects and arrays. **Special JS Feature/ Syntax: None** There are no special JavaScript features or syntaxes being used in this benchmark that would require additional explanation. **Other Alternatives** For comparing arrays, you can use other approaches like: 1. **Array.prototype.every() + ===**: Similar to the `JSON.stringify + ===` approach but using the `every()` method instead. 2. **Set-based comparison**: You can convert both arrays to sets and then compare them for equality. This approach is efficient for large datasets since set operations have a low overhead. 3. **Array.prototype.sort() + ===**: Sort both arrays and then compare them for equality. This approach takes into account element order differences, but may not be suitable for datasets where the original order matters. These alternatives can provide different trade-offs between performance, complexity, and ease of implementation compared to the approaches tested in this benchmark.
Related benchmarks:
Lodash.isEqual vs JSON.stringify Equality Comparison for Shallow Array of Strings.
Lodash.isEqual vs JSON.stringify Equality Comparison for Array With Strings And Objects
compare arrays (lodash 4.17.21)
compare arrays + toString
Comments
Confirm delete:
Do you really want to delete benchmark?