Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniqBy performance
(version: 0)
Comparing performance of:
lodash uniqBy vs javascript vs javascript 2
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script>https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.core.js</script>
Script Preparation code:
var data = [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }, { a: 5 }, { a: 6 }, { a: 7 }, { a: 8 }, { a: 1 }];
Tests:
lodash uniqBy
_.uniqBy(data, 'a');
javascript
const sortBy = (key) => { return (a, b) => (a[key] > b[key]) ? 1 : (b[key] > a[key] ? -1 : 0); }; const uniqBy = (arr, a) => { arr.sort(sortBy(a)); const arr1 = []; arr1.push(arr[0]); for (let index = 1; index < arr.length; index++) { if (arr[index-1] && arr[index -1].a !== arr[index].a) { arr1.push(arr[index]); } } return arr1; } uniqBy(data, 'a')
javascript 2
function uniqBy(arr, key) { let seen = new Set() return arr.filter(it => { let val = it[key] if (seen.has(val)) { return false } else { seen.add(val) return true } }) } uniqBy(data, 'a')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
lodash uniqBy
javascript
javascript 2
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):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, comparing different approaches to solve the same problem. The provided benchmark definition and test cases are for finding unique elements in an array based on a specific property. **Benchmark Definition** The benchmark definition is: `"_.uniqBy(data, 'a');"` This uses the Lodash library's `uniqBy` function, which removes duplicate elements from an array based on a specified property. The `_` symbol refers to the Lodash library. **Options Compared** There are three test cases that compare different approaches: 1. **Lodash `uniqBy`**: Uses the Lodash library's `uniqBy` function. 2. **Custom JavaScript implementation**: A custom JavaScript function named `uniqBy` is implemented to achieve the same result. 3. **Set-based approach**: Another custom JavaScript function uses a Set data structure to remove duplicates. **Pros and Cons of Each Approach** 1. **Lodash `uniqBy`**: * Pros: Highly optimized, widely used, and well-maintained library. * Cons: Adds an extra dependency (the Lodash library), may not be suitable for all use cases due to its specific behavior. 2. **Custom JavaScript implementation**: * Pros: No additional dependencies, can be tailored to specific requirements. * Cons: May not be as efficient or optimized as the Lodash version, requires more boilerplate code. 3. **Set-based approach**: * Pros: Efficient and simple, does not require any external libraries. * Cons: May have higher memory usage due to the Set data structure. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for common tasks, such as array manipulation, string manipulation, and more. The `uniqBy` function removes duplicate elements from an array based on a specified property, which matches the problem statement. **Custom JavaScript Implementation** The custom JavaScript implementation uses a simple approach to remove duplicates: sorting the array based on the specified property and then selecting every other element starting from the first one. This works by exploiting the fact that in a sorted array, consecutive elements with the same property value are adjacent. **Set-based Approach** This approach uses a Set data structure to keep track of unique elements seen so far. It iterates through the array, adding each element to the Set based on its property value. If an element is already in the Set, it is skipped; otherwise, it is added to the result array. **Other Alternatives** Some alternative approaches could include: * Using `Array.prototype.filter()` and `Array.prototype.indexOf()` * Utilizing a Map data structure instead of a Set * Implementing a more advanced algorithm, such as using a Trie or a Bloom filter However, these alternatives may not be as efficient or optimized as the Lodash version or the custom JavaScript implementation.
Related benchmarks:
uniqBy performance
uniqBy vs stringify performance
uniqBy performance ttt
uniqBy performance lodash vs native
uniqBy performance and map
Comments
Confirm delete:
Do you really want to delete benchmark?