Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash uniqBy vs Native
(version: 0)
Comparing performance of:
lodash vs native
Created:
2 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>
Tests:
lodash
const x = [{id: 1}, {id: 1}, {id: 2}, {id: 3}]; const y = _.uniqBy(x, 'id'); console.log(y);
native
const x = [{id: 1}, {id: 1}, {id: 2}, {id: 3}]; const y = x.reduce((arr, curr) => { const found = arr.find(a => a.id === curr.id); if (!found) arr.push(curr); return arr; }, []); console.log(y)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash
native
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash
564091.4 Ops/sec
native
571825.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and its pros/cons. **Benchmark Overview** The benchmark compares two approaches to remove duplicates from an array based on a specific property (`id` in this case). The two approaches are: 1. Using the `lodash` library (specifically, the `uniqBy` function). 2. Implementing the logic manually using JavaScript's built-in methods. **Test Cases** The benchmark consists of two test cases: ### 1. Lodash uniqBy vs Native **Benchmark Definition:** ```javascript const x = [{id: 1}, {id: 1}, {id: 2}, {id: 3}]; const y = _.uniqBy(x, 'id'); console.log(y); ``` This test case uses the `lodash` library to remove duplicates from the array `x`. The `_.uniqBy` function takes an array and a property key as arguments and returns a new array with unique elements based on that property. **Library:** `_` * **Purpose:** The Lodash library provides a collection of reusable JavaScript functions. In this case, `_.uniqBy` is used to remove duplicates from arrays. * **Pros:** + Simplifies code and reduces boilerplate. + Provides a robust and efficient implementation of array deduplication. * **Cons:** + Adds an external dependency (the Lodash library). + May introduce performance overhead due to the additional function call. ### 2. Native **Benchmark Definition:** ```javascript const x = [{id: 1}, {id: 1}, {id: 2}, {id: 3}]; const y = x.reduce((arr, curr) => { const found = arr.find(a => a.id === curr.id); if (!found) arr.push(curr); return arr; }, []); console.log(y); ``` This test case implements the logic manually to remove duplicates from the array `x`. The `reduce` method is used to iterate over the array and push unique elements to a new array. **Pros:** * No external dependencies. * Can be optimized for performance by using built-in methods. * Provides fine-grained control over the implementation. **Cons:** * More code to write and maintain. * May require more expertise in JavaScript's built-in methods. **Other Alternatives** If you wanted to implement an alternative approach, some options could include: 1. Using `Set` data structure to remove duplicates. 2. Utilizing a library like `array-deduplication`. 3. Implementing a custom implementation using recursion or iteration. However, these alternatives might not be as efficient or concise as the native JavaScript solution or the Lodash library solution. **Performance Considerations** The benchmark results show that the native approach outperforms the Lodash library approach in terms of executions per second. This is likely due to the fact that the native implementation avoids the overhead of function calls and uses built-in methods more efficiently. Keep in mind that performance can vary depending on the specific use case, data size, and other factors. It's essential to consider these factors when choosing an approach for your specific requirements.
Related benchmarks:
lodash uniq vs native uniq
uniqBy performance
get uniq values js
uniqBy performance lodash vs native
Lodash uniqBy vs Javascript uniqBy
Comments
Confirm delete:
Do you really want to delete benchmark?