Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.uniqBy vs myUniqBy
(version: 13)
_.uniqBy vs myUniqBy
Comparing performance of:
_.uniqBy vs myUniqBy
Created:
4 years ago
by:
Registered User
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 arr1 = Array.from({length: 1000000}, (v, i) => ({ name: i })); var concatArr = [].concat(arr1, { name: 999999 }); let result = null; function myUniqBy (arr, name) { let result = [] let obj = {} for (let i of arr) { if (!obj[i[name]]) { result.push(i) obj[i[name]] = i } } return result }
Tests:
_.uniqBy
result = _.uniqBy(concatArr, 'name');
myUniqBy
result = myUniqBy(concatArr, 'name');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.uniqBy
myUniqBy
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's being tested. **Benchmark Overview** The benchmark is comparing two functions: `_.uniqBy` from Lodash and a custom function called `myUniqBy`. Both functions are designed to remove duplicates from an array based on a specific property. **Function Comparison** In the provided JSON, we see two test cases: 1. `_.uniqBy(concatArr, 'name')`: This tests the original implementation of `_.uniqBy` from Lodash. 2. `myUniqBy(concatArr, 'name')`: This tests the custom implementation called `myUniqBy`. **Functionality Comparison** Both functions take an array and a property name as input. They iterate through the array, and for each element, they check if the corresponding value in the object (created by hashing the original value) is already present. If not, they add the element to the result array. Here's a high-level comparison of the two approaches: **_.uniqBy** Pros: * Built-in Lodash implementation, likely optimized for performance. * Likely tested with various edge cases and test frameworks. Cons: * Requires an external library (Lodash). * Might not be as customizable as the custom implementation. **myUniqBy** Pros: * Customizable: developers can modify or extend the logic to fit their specific use case. * No external dependencies. Cons: * Implementer's responsibility to optimize performance and handle edge cases. * May have slower execution times compared to the Lodash implementation. **Library: _.uniqBy (Lodash)** `.uniqBy` is a utility function from the Lodash library, which provides a convenient way to remove duplicates from an array based on a specific property. The original implementation of `_.uniqBy` likely uses a combination of hashing and iteration to achieve performance optimization. **Special JavaScript Features or Syntax** There are no special JavaScript features or syntax mentioned in the benchmark code. **Other Alternatives** If you prefer not to use Lodash, here are some alternatives: * You can implement your own duplicate-removing algorithm using simple loops and hash tables. * Other libraries like Underscore.js or Ramda.js provide similar functionality that might be worth exploring. * For a more efficient solution, you could consider using a data structure like a Map or an object with a custom key function to eliminate duplicates. Keep in mind that this benchmark is testing the performance of these two specific implementations. Depending on your project's requirements and constraints, you may need to choose between using an existing library (like Lodash) or implementing something custom.
Related benchmarks:
array concat
The Many Ways of Concatenating
Array merge (spread, concat)
spread vs concat vs unshift(clone)
Array.unshift vs Array.concat
Comments
Confirm delete:
Do you really want to delete benchmark?