Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
compare arrays 23
(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 result = true; let i = a1.length; if(i !== a2.length) { result = false; } else { while (i--) { if(a1[i] !== a2[i]) { result = false; break; } } } return result; }
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 dive into the world of MeasureThat.net and explore what's being tested in this benchmark. **Benchmark Overview** The benchmark compares the performance of six different methods for comparing two arrays: 1. `_.isEqual` from Lodash 2. `JSON.stringify` with equality check 3. `equals` (a custom implementation) 4. `equalsIgnoreOrder` (another custom implementation) 5. `arrIdentical` (a custom implementation) **Lodash _.isEqual** `.isEqual` is a function that checks if two values are equal, considering both value and type. It's a useful utility function in Lodash that can be used for comparing objects or arrays. **JSON.stringify with equality check** This method uses `JSON.stringify` to convert the arrays to strings and then compares these strings using the `===` operator. This approach has performance implications due to the overhead of string conversion and comparison. **equals** The `equals` function is a custom implementation that checks if two arrays have the same elements, regardless of order. It's likely implemented as an iteration-based loop. **equalsIgnoreOrder** The `equalsIgnoreOrder` function is another custom implementation that's similar to `equals`, but it ignores the order of elements when comparing the arrays. This approach might be useful for certain use cases where the order of elements doesn't matter. **arrIdentical** The `arrIdentical` function checks if two arrays have exactly the same elements, including order. It's likely implemented using a simple iteration-based loop. **Performance Comparison** The benchmark results show the performance of each method across multiple executions per second. The top performers are: 1. `arrForLoop` (custom implementation) - 915,289 executions/second 2. `equals` (custom implementation) - 913,693 executions/second 3. `arrIdentical` (custom implementation) - 885,795 executions/second The Lodash implementation, `.isEqual`, is relatively slow compared to the custom implementations. **Conclusion** In this benchmark, we see that custom implementations can outperform popular libraries like Lodash when it comes to comparing arrays. The performance difference lies in the overhead of library functions and string conversions. However, the choice of implementation ultimately depends on the specific use case and personal preference.
Related benchmarks:
lodash flatmap long
lodash flatmap longest
compare arrays 22
Lodash difference vs filter and includes on large arrays
Comments
Confirm delete:
Do you really want to delete benchmark?