Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
unique objects in array by value using filter
(version: 2)
Comparing performance of:
_.uniqBy vs Set vs findIndex vs reduce
Created:
6 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
Script Preparation code:
var analysisAuthors = [{"studyField":"novel","authorName":"Apollinaire"},{"studyField":"novel","authorName":"Apollinaire"},{"studyField":"novel","authorName":"Apollinaire"},{"studyField":"novel","authorName":"Apollinaire"},{"studyField":"novel","authorName":"Apollinaire"},{"studyField":"novel","authorName":"Apollinaire"},{"studyField":"novel","authorName":"De Lafayette"},{"studyField":"novel","authorName":"Beckett"},{"studyField":"novel","authorName":"Montesquieu"},{"studyField":"novel","authorName":"Yourcenar"}]
Tests:
_.uniqBy
_.uniqBy(analysisAuthors, "authorName")
Set
Array.from(new Set(analysisAuthors.map(a => a.authorName))) .map(authorName => { return analysisAuthors.find(a => a.authorName === authorName) })
findIndex
analysisAuthors.filter((author, index, authorsList) => authorsList .findIndex(authorInList => authorInList.authorName === author.authorName) === index)
reduce
analysisAuthors.reduce((acc, current) => { const x = acc.find(item => item.authorName === current.authorName); if (!x) { return acc.concat([current]); } else { return acc; } }, [])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
_.uniqBy
Set
findIndex
reduce
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 designed to measure the performance of different approaches to remove duplicates from an array based on a specific property (in this case, `authorName`). The benchmark uses a predefined dataset (`analysisAuthors`) and compares four different methods: 1. Using Lodash's `_.uniqBy` function 2. Converting the array to a set and then mapping over it 3. Using `Array.prototype.filter` with `findIndex` 4. Using `Array.prototype.reduce` **Options Comparison** Here's a brief overview of each option: ### 1. Lodash's _.uniqBy * **Purpose**: Removes duplicates from an array based on a specified property. * **Pros**: Efficient, concise, and well-documented. * **Cons**: May not be suitable for very large datasets due to the overhead of the Lodash library. ### 2. Converting to Set and Mapping * **Purpose**: Similar to _.uniqBy, but uses native JavaScript features instead of a library. * **Pros**: Lightweight, efficient, and can handle larger datasets. * **Cons**: May be less concise than using a library like Lodash. ### 3. findIndex in Array.prototype.filter * **Purpose**: Removes duplicates from an array by filtering out elements that have already been seen. * **Pros**: Efficient, easy to understand, and works well with large datasets. * **Cons**: Can be slower for very large datasets due to the overhead of the `findIndex` method. ### 4. Array.prototype.reduce * **Purpose**: Similar to findIndex in Array.prototype.filter, but uses a cumulative reduction approach. * **Pros**: Efficient, easy to understand, and works well with large datasets. * **Cons**: Can be slower for very small datasets due to the overhead of the reduce method. **Library: Lodash** Lodash is a popular JavaScript library that provides a collection of useful functions for working with arrays, objects, and more. In this benchmark, Lodash's `_.uniqBy` function is used to remove duplicates from the array based on the `authorName` property. **Special JS Feature/Syntax: None** There are no special JavaScript features or syntaxes being tested in this benchmark. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * Using `Map` instead of Lodash's `_.uniqBy` * Using `Array.prototype.forEach` and manual iteration to remove duplicates * Using a custom implementation without relying on any libraries or built-in methods Keep in mind that these alternatives may have varying trade-offs in terms of performance, conciseness, and readability.
Related benchmarks:
Unique values
Filter vs Set (get unique elements)
Unique names include vs indexOf
test12345
Javascript uniqueBy key vs Javascript uniqueBy stringify
Comments
Confirm delete:
Do you really want to delete benchmark?