Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
uniqBy vs Set
(version: 0)
Comparing performance of:
uniqBy vs Set
Created:
3 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 Preparation code:
var files = []; for (i = 0; i < 1000; i++) { files.push({ documentId: i, documentVersionId: i + 1 }); } var filesToAdd = []; for (i = 0; i < 50; i++) { filesToAdd.push({ documentId: i * 2, documentVersionId: i + i * 3 }); }
Tests:
uniqBy
const uniqueDocs = _.uniqBy([...files, ...filesToAdd], 'documentVersionId'); console.log(uniqueDocs.length);
Set
const uniqueDocs = new Set([...files, ...filesToAdd]); console.log(uniqueDocs.size);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
uniqBy
Set
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 dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided benchmark compares two approaches to remove duplicates from an array of objects based on a specific property (`documentVersionId`). The test consists of two individual tests: `uniqBy` and `Set`. **Options Compared** 1. **_.uniqBy (Lodash)**: * Pros: + Uses a functional programming approach, which can be more concise and expressive. + Lodash is a well-maintained and widely used library with extensive testing. * Cons: + Requires an external dependency (Lodash). + May have overhead due to the use of a separate library. 2. **Set**: * Pros: + Native JavaScript implementation, no external dependencies required. + Can be optimized for performance by using a specialized data structure (HashSet). * Cons: + May require manual implementation of duplicate removal logic. + Less concise and expressive than _.uniqBy. **Additional Considerations** * **Performance**: The benchmark shows that `Set` outperforms `_ uniqBy` in this specific test case. However, the performance difference may not be significant for all use cases. * **Code Readability**: _.uniqBy is generally more readable and maintainable due to its concise and expressive syntax. **Library Used** In the provided benchmark, Lodash is used as a dependency to implement `_.uniqBy`. The library provides a `uniqBy` function that takes an array of objects and a property to consider for uniqueness. The function returns an array with only unique elements based on the specified property. **JavaScript Feature or Syntax (Not Applicable)** Since neither test case uses any special JavaScript features or syntax, we can ignore this aspect. **Other Alternatives** 1. **Built-in `filter()` method**: While not as efficient as using a Set, you could use the `filter()` method to remove duplicates from an array. 2. **Manual implementation**: You could implement your own duplicate removal logic using a simple loop or recursion. 3. **Other libraries**: Other JavaScript libraries like Moment.js or Underscore.js might provide similar functionality for removing duplicates. In summary, while both approaches have their pros and cons, `Set` appears to be the more efficient choice in this specific benchmark. However, _.uniqBy remains a viable option due to its concise syntax and extensive testing.
Related benchmarks:
lodash uniq vs native uniq
uniqBy vs stringify performance
uniq-vs-uniqWith
Lodash uniqBy vs Reduce uniqBy
uniqBy performance lodash vs native
Comments
Confirm delete:
Do you really want to delete benchmark?