Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
compare arrays (lodash 4.17.21)
(version: 0)
lodash vs json stringify and other methods
Comparing performance of:
_.isEqual vs JSON.stringify vs equals vs equalsIgnoreOrder
Created:
2 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/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:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.isEqual
1243392.9 Ops/sec
JSON.stringify
685393.2 Ops/sec
equals
1628782.4 Ops/sec
equalsIgnoreOrder
835004.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON. **Benchmark Definition:** The benchmark is comparing three approaches to check if two arrays are equal: 1. `_.isEqual` (from Lodash library) 2. `JSON.stringify` with a string comparison (`===`) 3. `equals` and `equalsIgnoreOrder` custom functions defined in the script preparation code These methods will be compared to determine which one is the fastest. **Options being compared:** * **Lodash's _.isEqual**: A utility function designed to compare two values for equality, taking into account various aspects such as order, type, and primitive values. * **JSON.stringify with string comparison (`===`)**: A simple approach that converts both arrays to strings using `JSON.stringify()` and then compares the resulting strings. This method only checks if the elements are equal in a specific order ( lexicographical order). * **equals and equalsIgnoreOrder custom functions**: These two functions aim to provide more nuanced equality comparisons than _.isEqual. **Pros and Cons:** 1. **_.isEqual**: * Pros: Handles various edge cases, like arrays with different lengths or orders, and primitive values. * Cons: May have a higher overhead due to its extensive logic. 2. **JSON.stringify with `===`**: * Pros: Simple, fast, and easy to implement. * Cons: Ignores order and may not work correctly for some edge cases (e.g., NaN vs. NaN). 3. **equals and equalsIgnoreOrder custom functions**: * Pros: Provide more flexibility in handling different equality scenarios, like ignoring order or primitive values. * Cons: More complex implementation can lead to higher overhead. **Library:** The Lodash library is being used for the _.isEqual function. **Special JS features/syntax:** None mentioned. **Other alternatives:** 1. Other array comparison functions or libraries (e.g., Array.prototype.every(), Array.prototype.filter()) could be considered as alternatives. 2. More advanced equality comparison techniques, such as using a hash table to store elements and then comparing the stored values. When choosing an approach for array equality comparisons, consider factors like performance, edge case handling, and code simplicity. _.isEqual from Lodash provides a good balance between these aspects, but custom implementations or alternative libraries might be necessary depending on specific requirements.
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
compare arrays + toString
Comments
Confirm delete:
Do you really want to delete benchmark?