Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.isEqual vs object-hash duplicate data detection for array of objects with nested properties and lots of records 13
(version: 0)
Comparing performance of:
_.isEqual vs JSON.stringify vs objectHash
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/object-hash@2.0.3/dist/object_hash.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
window.NUM_ITEMS = 1000; window.NUM_TAKE = 20; window.input = []; window.data = []; window.check = []; window.indices = [...Array(window.NUM_ITEMS).keys()]; // Populate data: entries with a nested array of 50 random entries for (const i of window.indices) { window.data[i] = { ids: window.indices.slice(0, 50).map((x) => Math.floor(Math.random() * window.NUM_ITEMS * 10)), counter: i, counter2: Math.floor(Math.random() * i), }; window.check[i] = false; } // calculate hashes for each data object window.hashes = new Set(window.data.map((obj) => objectHash(obj))); window.strings = new Set(window.data.map(JSON.stringify)); window.TAKE_IDX = window.indices.reverse().slice(0, window.NUM_TAKE); // Uncomment to choose random indices //window.TAKE_IDX = window.TAKE_IDX.map((x) => Math.floor(Math.random() * window.NUM_ITEMS)); // Populate input: copies of the data for (const i of window.TAKE_IDX) { window.input[i] = { ids: [...window.data[i].ids], counter: window.data[i].counter, counter2: window.data[i].counter2 }; }
Tests:
_.isEqual
for (const i of window.TAKE_IDX) { window.check[i] = window.data.some((item) => _.isEqual(item, window.input[i])); }
JSON.stringify
for (const i of window.TAKE_IDX) { window.check[i] = window.strings.has(JSON.stringify(window.input[i])); }
objectHash
for (const i of window.TAKE_IDX) { window.check[i] = window.hashes.has(objectHash(window.input[i])); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.isEqual
JSON.stringify
objectHash
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):
I'll break down the provided benchmark definition and explain what's being tested, compared, and their pros/cons. **Benchmark Overview** The benchmark compares three approaches to detect duplicate data in an array of objects with nested properties: 1. `_.isEqual` (Lodash) 2. `JSON.stringify` 3. `objectHash` The test case uses a large dataset (`window.NUM_ITEMS = 1000`) and generates random data with nested arrays. **Approaches Comparison** Here's a brief overview of each approach: ### _.isEqual (Lodash) * **Purpose**: Compare two objects for equality using a deep comparison algorithm. * **Pros**: + Fast execution speed. + Easy to implement. * **Cons**: + Can be slow for very large datasets due to its recursive nature. + May not work correctly with certain data structures (e.g., circular references). ### JSON.stringify * **Purpose**: Compare two objects by converting them to a string and checking if the resulting strings are equal. * **Pros**: + Fast execution speed, especially for smaller datasets. + Works well with most data types. * **Cons**: + May not work correctly with nested arrays or circular references. + Can be slower than _.isEqual for very large datasets. ### objectHash * **Purpose**: Generate a unique hash value for each object based on its properties and values. * **Pros**: + Fast execution speed, especially for larger datasets. + Works well with nested arrays and most data types. * **Cons**: + Requires an external library (object-hash). + May produce collisions if not designed correctly. **Other Considerations** * **Data Structure**: The benchmark uses a large array of objects with nested arrays, which can make it challenging to detect duplicates using some approaches. * **Platform and Browser**: The benchmark results are reported for Chrome 119 on Windows Desktop, but the performance may vary across different browsers and platforms. **Alternatives** If you're interested in exploring alternative approaches, here are a few options: 1. **Array.prototype.indexOf()**: This method checks if an element exists in an array using its index value. You can use this approach to detect duplicates by iterating through the array and checking for existing indices. 2. **Set data structure**: Using a Set data structure can help efficiently detect duplicate elements, especially when working with arrays of unique values. 3. **Other hash-based approaches**: There are other libraries and algorithms available that generate hashes for objects, such as `crypto` module in Node.js or `hash-object` library. Keep in mind that the best approach depends on your specific use case and requirements.
Related benchmarks:
_.uniq() vs Set() over large array
Array Intersection vs. Set Intersection vs. Lodash
Array Intersection vs. Set Intersection vs. Lodash part 3
Array Intersection vs. Set Intersection vs. Lodash - big
lodash uniq vs Array.from(new Set()) vs spread new Set() [big arrays 2]
Comments
Confirm delete:
Do you really want to delete benchmark?