Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unique Values in Array
(version: 0)
Comparing performance of:
my uniquesness vs lodash
Created:
5 years ago
by:
Guest
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 type="text/javascript"> window.lodash = _; _ = null; </script>
Script Preparation code:
var data = [{ 'id': 1, 'value': 'one' }, { 'id': 2, 'value': 'two' }, { 'id': 3, 'value': 'three' }, { 'id': 4, 'value': 'four' }, { 'id': 4, 'value': 'four' }, { 'id': 5, 'value': 'five' }]; function cleanSelectedDataForUniqueness(selectedData) { const _selectedDataSet = new Set(selectedData.map((el) => JSON.stringify(el))); return Array.from(_selectedDataSet).map((el) => JSON.parse(el)); };
Tests:
my uniquesness
cleanSelectedDataForUniqueness(data);
lodash
lodash.uniqWith(data,lodash.isEqual);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
my uniquesness
lodash
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 provided benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark measures the performance of two different approaches to find unique values in an array: 1. **Custom implementation**: `cleanSelectedDataForUniqueness(data)`, which uses a Set data structure to remove duplicates. 2. **Lodash library**: `lodash.uniqWith(data, lodash.isEqual)`. **Custom Implementation (Set-based)** This approach uses a Set data structure to store unique values from the input array. Here's what happens: * The input array is mapped to JSON strings using `JSON.stringify(el)`. * A Set is created with these JSON strings. * The Set is then converted back to an array of objects using `Array.from()` and `JSON.parse(el)`. Pros: * Efficient use of memory, as Sets only store unique values. * Fast lookup times, as Sets allow for O(1) membership testing. Cons: * Requires manual handling of JSON string conversions, which might lead to errors if not done correctly. * May have performance overhead due to the conversion from Set to array. **Lodash Library (uniqWith)** The Lodash library provides a `uniqWith` function that takes two arguments: an array and a callback function. Here's how it works: * The `lodash.isEqual` function is used as the callback, which checks for deep equality between objects. * The input array is passed to `lodash.uniqWith`, which removes duplicates by checking each element against the previous ones using `lodash.isEqual`. Pros: * Convenient and concise way to remove duplicates from an array without manual implementation. * Built-in support for handling complex data structures. Cons: * May have performance overhead due to the use of a callback function, which can lead to slower execution times. * Requires importing Lodash library, which might add size and complexity to the benchmark. **Other Considerations** The benchmark also includes a script that prepares the input data by creating an array with duplicate values. This is done to simulate real-world scenarios where data may contain duplicates. Additionally, the benchmark uses `window.lodash = _;` to set up Lodash as a global variable, allowing it to be accessed in subsequent JavaScript code. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. However, it's worth noting that the use of Set and JSON string conversions might require careful handling to ensure correctness. **Alternatives** Other alternatives for removing duplicates from an array include: * Using `filter()` method with a callback function: `data.filter((el) => data.indexOf(el) === data.lastIndexOf(el))` * Using `reduce()` method with a callback function: `data.reduce((acc, el) => acc.includes(el) ? acc : [el])` These alternatives might have different performance characteristics and memory usage compared to the custom implementation or Lodash library.
Related benchmarks:
Find item in array
Find item in array - Fork
remove duplicate
returns unique values in array
Uniq by sorting test
Comments
Confirm delete:
Do you really want to delete benchmark?