Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.js vs Native Remove Duplicates
(version: 0)
Comparing performance of:
Native vs Lodash.js filter
Created:
5 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:
var max1 = 100000; // 100,000 (100 Thousand) var max2 = 10000000; // 10,000,000 (10 Million) var max3 = 100000000; // 100,000,000 (100 Million) var arr1 = []; //for (var i = 0; i <= max1; i++) { arr1.push(i); } var arr2 = []; for (var i = 0; i <= max2; i++) { arr2.push({name: i, value: Math.floor(Math.random() * 100) }); } var arr3 = []; //for (var i = 0; i <= max3; i++) { arr3.push(i); }
Tests:
Native
const removeDuplicates = (objects, propertyName) => { const uniqueValues = new Set(); return objects.filter((o) => { const isNewValue = !uniqueValues.has(o[propertyName]); uniqueValues.add(o[propertyName]); return isNewValue; }); }; removeDuplicates(arr2, 'value')
Lodash.js filter
const removeDuplicates = (objects, propertyName) => { _.uniqBy(objects, propertyName) }; removeDuplicates(arr2, 'value')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Native
Lodash.js filter
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):
**Benchmark Overview** The provided JSON represents a benchmark test case created on the MeasureThat.net website, which compares the performance of two approaches to remove duplicates from an array: a native JavaScript implementation and a Lodash.js function. **Native JavaScript Implementation** The native JavaScript implementation uses the `filter()` method to remove duplicates. It creates a `Set` object (`uniqueValues`) to keep track of unique values. The `filter()` method iterates through the array, and for each element, it checks if the value is new by checking if it's not in the `Set`. If it's new, the value is added to the `Set`, and the element is included in the resulting array. Pros: * Native JavaScript implementation does not rely on external libraries. * Easy to understand and implement. Cons: * Performance may be slower compared to optimized library implementations. * May require more memory due to the use of a `Set` object. **Lodash.js Implementation** The Lodash.js implementation uses the `_uniqBy()` function from the Lodash library. This function takes two arguments: an array (`objects`) and a property name (`propertyName`). It returns a new array with duplicates removed based on the specified property. Pros: * Optimized for performance. * Often used in production code, so it's well-tested and maintained. Cons: * Requires the inclusion of an external library (Lodash.js). * May have overhead due to the need to load and initialize the Lodash library. **Library: Lodash.js** Lodash is a popular JavaScript utility library that provides a collection of functional programming helpers. The `_uniqBy()` function is one of these helpers, designed to remove duplicates from an array based on a specified property. The `_.uniqBy()` function uses a combination of the `Set` data structure and the `filter()` method to achieve its performance benefits. This approach ensures that the function can scale well for large datasets while maintaining efficient execution times. **Special JS Feature/Syntax** There is no special JavaScript feature or syntax used in this benchmark test case. The implementation relies on standard JavaScript features, such as the `Set` object and array methods like `filter()` and `push()`. **Alternatives** If you want to implement a similar benchmark or test the performance of other approaches to remove duplicates from an array, here are some alternatives: 1. **Using a custom implementation with a data structure other than a Set**: Instead of using a `Set`, you could use another data structure like a `Map` or a binary search tree (BST) to keep track of unique values. 2. **Using a different library or framework**: Some libraries, such as Ramda.js or Lo-Dash, offer similar functionality to Lodash.js for removing duplicates from arrays. 3. **Implementing the benchmark using a testing framework**: Instead of writing your own benchmark code, you could use an existing testing framework like Jest or Mocha to write and execute your benchmarks. Keep in mind that each alternative has its pros and cons, and the choice ultimately depends on your specific requirements, performance considerations, and personal preferences.
Related benchmarks:
Lodash.js vs Native isArrary
Lodash.js vs Native _.min
Lodash.js vs Native MAGIC
Lodash.js vs Native random2
Comments
Confirm delete:
Do you really want to delete benchmark?