Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array of objects equivalence (custom vs lodash)
(version: 0)
Comparing performance of:
lodash vs custom
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
function equivalent(arr1, arr2) { if (arr1.length !== arr2.length) { return false } arr1.forEach((elem) => { const index = arr2.findIndex((elem2) => elem === elem2) if (index === -1) { // not found return false } arr2.splice(index, 1) // remove element }) if (arr2.length > 0) { // if still values in arr2, they are not in arr1 return false } return true } function _equiv(arr1, arr2) { return _.isEqual(_.sortBy(arr1), _.sortBy(arr2)) }
Tests:
lodash
const arr1 = [{ KMFGNHeL: 0.2033444758693359, HJMzVLhZ: 373, WeKLrVwh: 'BlgvHn', hutdufbF: 'dfalRd', XqrwJjnR: null }, { xSUdtASV: 'ejVI', pgUHOMNn: true, dhQPzxuj: false, MHUchilq: 719, TWHkCOOT: 0.46691745627158165 }] const arr2 = [{ fZhKYyQT: 379, WtnguXmc: 64, VaodnYbC: 0.7490618732830732, hGhDbXmd: { pQnhfhKR: 0.029586390538915097, oSpBStGM: { dKoUrTyg: 0.8023980228055398, RMSlVEQw: null, KiPTRmIf: 'GIfgX', OOpGOzXD: null, YSTcLKDa: 777 }, eDsjVaPK: null, LrqwYwJs: 0.4117401997875565, AbIoXBix: 'THmFh' }, anQZVYxA: 0.8284963046084715 }] _equiv(arr1, arr2)
custom
const arr1 = [{ KMFGNHeL: 0.2033444758693359, HJMzVLhZ: 373, WeKLrVwh: 'BlgvHn', hutdufbF: 'dfalRd', XqrwJjnR: null }, { xSUdtASV: 'ejVI', pgUHOMNn: true, dhQPzxuj: false, MHUchilq: 719, TWHkCOOT: 0.46691745627158165 }] const arr2 = [{ fZhKYyQT: 379, WtnguXmc: 64, VaodnYbC: 0.7490618732830732, hGhDbXmd: { pQnhfhKR: 0.029586390538915097, oSpBStGM: { dKoUrTyg: 0.8023980228055398, RMSlVEQw: null, KiPTRmIf: 'GIfgX', OOpGOzXD: null, YSTcLKDa: 777 }, eDsjVaPK: null, LrqwYwJs: 0.4117401997875565, AbIoXBix: 'THmFh' }, anQZVYxA: 0.8284963046084715 }] equivalent(arr1, arr2)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash
custom
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 the benchmark and explain what's being tested, compared, and their pros and cons. **What's being tested?** The benchmark is testing two approaches to check if two arrays of objects are equivalent: 1. **Custom implementation**: The `equivalent` function checks each element of both arrays for equality by using `findIndex` to find the index of a matching object in the second array, and then removing that element from the second array. If any elements are not found, it returns false. 2. **Lodash library**: The `_equiv` function uses the Lodash library's `isEqual` function to check if two arrays of objects are equivalent. **Options compared** The benchmark is comparing the performance of these two approaches: * Custom implementation * Lodash library **Pros and Cons:** 1. **Custom implementation**: * Pros: + May be more efficient since it doesn't require loading an external library. + Can provide insight into the performance characteristics of a custom implementation. * Cons: + Requires manual implementation, which can be error-prone and time-consuming. + May not be as robust or flexible as a well-maintained library like Lodash. 2. **Lodash library**: * Pros: + Provided by a widely-used and maintained library, reducing the chance of errors. + Often includes optimized implementations for common use cases. * Cons: + Requires loading an external library, which can add overhead. + May not be as efficient as a custom implementation that's specifically tailored to this use case. **Other considerations** 1. **Lodash library**: Lodash is a popular utility library for JavaScript that provides many useful functions for working with arrays, objects, and more. In this benchmark, the `_equiv` function uses `isEqual`, which compares two values using a deep equality check. 2. **Custom implementation**: The custom implementation assumes that all elements in both arrays are objects with similar properties. If this is not the case, the comparison may not work correctly. **Alternatives** Other alternatives to consider when checking array equivalence include: 1. Using a different library, such as `lodash-es` or `ramda`, which offer alternative equality checks. 2. Implementing a custom equality check using a different algorithm or data structure, such as a hash table or a trie. 3. Using a testing framework like Jest or Mocha to write and run automated tests for the custom implementation. In conclusion, this benchmark is testing two approaches to checking array equivalence: a custom implementation and a Lodash library-based approach. The pros and cons of each approach highlight the trade-offs between performance, efficiency, and maintainability.
Related benchmarks:
_.filter vs Array.filter
Intersection filter vs lodash intersection
Intersection filter vs lodash intersection test unsorted array
lodash differenceby vs sort2
Lodash uniqBy vs Javascript uniqBy
Comments
Confirm delete:
Do you really want to delete benchmark?