Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Finding duplicates
(version: 0)
Comparing performance of:
Filter vs Group by
Created:
5 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 max = 5000; var arr = []; for (let i = 0; i < max; i++) { arr.push({ value: i}); // create an occasional duplicate if (i % 10 === 0) { arr.push({ value: i}); } }
Tests:
Filter
var duplicates = arr.filter((item, _, items) => items.find(dupItem => dupItem.value === item.value));
Group by
var grouped = _.groupBy(arr, item => item.value); var duplicates1 = _.flatten(_.filter(grouped, group => group.length > 1));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Filter
Group by
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 JSON data and explain what's being tested. **Benchmark Definition** The benchmark definition is a JavaScript script that creates an array `arr` of 5000 objects, where each object has a unique value between 0 and 4999. Every 10th iteration, another duplicate object with the same value is added to the array. The script preparation code is executed before running the benchmark, and it sets up the initial conditions for the benchmark. **Html Preparation Code** The HTML preparation code includes a link to a CDN (Content Delivery Network) hosted version of the Lodash library, which provides various utility functions. In this case, the `lodash.min.js` file is included. **Individual Test Cases** There are two individual test cases: 1. **Filter** This test case uses the built-in JavaScript `filter()` method to find duplicates in the array. The filter function takes an item from the array and checks if any other item has the same value as this item using a closure. However, it seems that there's a bug in this implementation because the callback function is missing its first argument (the current item) and its second argument (the index of the current item). The actual implementation should be `arr.filter((item, index, array) => { ... });`. 2. **Group by** This test case uses the Lodash library to group the array by value and then filter out groups with a length greater than 1. **Library: Lodash** Lodash is a popular utility library for JavaScript that provides various functions for tasks like string manipulation, array manipulation, and more. In this case, `groupBy()` is used to group the array by value. The `flatten()` function is also used in conjunction with `filter()` to further process the grouped data. **Special JS Feature/Syntax** There's no special JavaScript feature or syntax being tested here. The code is standard JavaScript, and it doesn't utilize any advanced features like async/await, Promises, or modern JavaScript syntax. **Other Alternatives** If you wanted to implement this benchmark without using Lodash, you could achieve similar results by writing a custom function to group the array by value and filter out groups with a length greater than 1. Here's an example: ```javascript function groupBy(arr, key) { const result = {}; arr.forEach((item) => { if (!result[item[key]]) { result[item[key]] = []; } result[item[key]].push(item); }); return Object.keys(result).map((key) => result[key]); } function findDuplicates(arr) { const grouped = groupBy(arr, 'value'); return _.flatten(grouped.filter((group) => group.length > 1)); } ``` However, using a library like Lodash can simplify the code and make it more efficient, especially for larger datasets.
Related benchmarks:
Unique lodash vs vanilla
lodash uniq vs spread new Set() medium size
JS fastest unique array Set vs uniq vs filter
Uniq by sorting test 2
Comments
Confirm delete:
Do you really want to delete benchmark?