Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
compare arrays 22
(version: 0)
lodash vs json stringify and other methods
Comparing performance of:
_.isEqual vs JSON.stringify vs equals vs equalsIgnoreOrder vs arrIdentical vs arrForLoop
Created:
3 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 = [1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4"]; window.bar = [1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4", 1,2,3,"4"]; 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; } window.arrIdentical = (a1, a2) => { // Tim Down: http://stackoverflow.com/a/7837725/308645 var i = a1.length; if (i != a2.length) return false; while (i--) { if (a1[i] !== a2[i]) return false; } return true; } window.arrForLoop = (a1, a2) => { let i = a1.length; if(i !== a2.length) { return false; } while (i--) { if(a1[i] !== a2[i]) { 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)
arrIdentical
arrIdentical(window.foo, window.bar)
arrForLoop
arrForLoop(window.foo, window.bar)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
_.isEqual
JSON.stringify
equals
equalsIgnoreOrder
arrIdentical
arrForLoop
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON benchmark. **Benchmark Goal** The goal is to compare two arrays, `window.foo` and `window.bar`, using different methods. **Comparison Options** 1. **_.isEqual(window.foo, window.bar)**: This method is a part of the Lodash library, which provides a robust set of functional programming tools. 2. **JSON.stringify(window.foo) === JSON.stringify(window.bar)**: This method uses the built-in `JSON.stringify()` function to convert both arrays into JSON strings and then compares these strings for equality. 3. **window.equals(window.foo, window.bar)**: This is a custom implementation that checks if both arrays have the same length and contain the same values in the same order. 4. **window.equalsIgnoreOrder(window.foo, window.bar)**: Similar to `window.equals()`, but it ignores the order of elements when comparing arrays. 5. **arrIdentical(window.foo, window.bar)**: This method uses a custom implementation that checks if both arrays have the same length and contain the same values in the same order (case-sensitive). 6. **arrForLoop(window.foo, window.bar)**: This method uses a traditional for-loop to iterate through both arrays and compare their elements. **Key Insights** * The `JSON.stringify()` method can be slow because it involves converting JavaScript objects to strings, which can be computationally expensive. * The custom implementations (`window.equals()`, `window.equalsIgnoreOrder()`) are generally faster than the Lodash implementation (`_.isEqual()`) because they avoid the overhead of string conversion and instead focus on comparing arrays directly. * The `arrIdentical()` method is slower than the other array comparison methods because it uses a traditional for-loop, which is less efficient than using built-in array comparison methods. **Benchmark Results** The latest benchmark results show that: * `arrForLoop()` is the slowest among all options (likely due to its use of a traditional for-loop). * `JSON.stringify()` is also relatively slow. * The custom implementations (`window.equals()`, `window.equalsIgnoreOrder()`) are faster than Lodash, but slower than `arrIdentical()`. **Conclusion** The choice of method depends on the specific requirements and performance considerations. If speed is a top priority, `arrIdentical()` might be a good option. However, if ease of use and maintainability are more important, one of the custom implementations or Lodash's `.isEqual()` method could be a better fit.
Related benchmarks:
lodash flatmap long
lodash flatmap longest
compare arrays 23
Lodash difference vs filter and includes on large arrays
Comments
Confirm delete:
Do you really want to delete benchmark?