Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Finding unique properties from objects
(version: 0)
Find fastest way to fetch unique properties from array of objects
Comparing performance of:
Map then create Set vs Map then use Lodash uniq vs Use Lodash then map
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js'></script>
Script Preparation code:
var arr = [{"id": 1, "name":"Name1", "country":"USA"}, {"id": 2, "name":"Name2", "country":"Scotland"}, {"id": 3, "name":"Name3", "country":"USA"}, {"id": 4, "name":"Name4", "country":"USA"}, {"id": 5, "name":"Name5", "country":"Scotland"}, {"id": 6, "name":"Name6", "country":"Poland"}, {"id": 7, "name":"Name7", "country":"USA"}];
Tests:
Map then create Set
return new Set(arr.map((o) => o.country));
Map then use Lodash uniq
return _.uniq(arr.map((o) => o.country));
Use Lodash then map
return _.uniqBy(arr, 'country').map((o) => o.country);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Map then create Set
Map then use Lodash uniq
Use Lodash then map
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 provided JSON data and explain what is tested, the options compared, pros and cons of those approaches, and other considerations. **Benchmark Definition** The benchmark tests finding unique properties from an array of objects. The script preparation code defines an array `arr` containing six objects with a "country" property each. The html preparation code includes a reference to Lodash library (`lodash.min.js`) which will be used in the benchmark test cases. **Test Cases** There are three individual test cases: 1. **Map then create Set**: This approach maps each object in the `arr` array to its "country" property using `map()`, and then creates a new set from the resulting array using `Set()`. 2. **Map then use Lodash uniq**: This approach uses the `uniq()` function from Lodash library to find unique values in the mapped array. 3. **Use Lodash then map**: This approach first uses the `uniqBy()` function from Lodash library to find unique objects based on their "country" property, and then maps each object to its "country" property. **Options Compared** The three test cases compare different approaches to finding unique properties: * Creating a set directly from the mapped array (Map then create Set) * Using `uniq()` function from Lodash library * First using `uniqBy()` function from Lodash library and then mapping each object to its "country" property **Pros and Cons** Here's a brief analysis of the pros and cons of each approach: 1. **Map then create Set**: * Pros: Simple, efficient, and lightweight. * Cons: May have higher overhead due to set creation and comparison operations. 2. **Map then use Lodash uniq**: * Pros: Uses optimized Lodash function for uniqueness detection. * Cons: Adds an external dependency (Lodash) and may introduce performance overhead due to library call. 3. **Use Lodash then map**: * Pros: Allows for more control over uniqueness detection using `uniqBy()` function. * Cons: May have higher overhead due to additional Lodash function calls. **Other Considerations** * The benchmark uses a relatively small input array, which may not accurately represent the performance characteristics of these approaches with larger datasets. * The use of Lodash library introduces an external dependency and may affect the test's overall execution time. * The benchmark is run on Firefox 89 browser on a desktop platform, so results may not be directly applicable to other browsers or platforms. **Alternatives** Other alternatives could include: 1. Using `Set` constructor with array comprehension: `new Set([o.country for o in arr])` 2. Using `Filter()` and `Reduce()` functions from Lodash library 3. Implementing a custom uniqueness detection function using Array.prototype.filter() and Set data structure. However, these alternatives may not be as efficient or optimized as the original approaches tested by MeasureThat.net.
Related benchmarks:
lodash_array_objects
lodash_array_objects_2
Finding duplicates
Array of strings, null, and ints lodash uniq vs set
Comments
Confirm delete:
Do you really want to delete benchmark?