Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
loadash vs set eg1
(version: 0)
Comparing performance of:
intersection vs set
Created:
4 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 first = []; var second = []; for(var i=0; i<10000; i++) { first.push({batchId: Math.floor(Math.random() * 100) , userId: Math.floor(Math.random() * 100)}) second.push({batchId: Math.floor(Math.random() * 100) , userId: Math.floor(Math.random() * 100)}) }
Tests:
intersection
_.intersection(first, second)
set
const firstSet = new Set(first); const secondSet = new Set(second); new Set([...firstSet].filter(item => secondSet.has(item)));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
intersection
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 break down the provided JSON that represents the JavaScript microbenchmark. **Benchmark Overview** The benchmark measures two approaches to intersecting two arrays: using Lodash (`_.intersection`) and using Sets with filtering. The test aims to compare the performance of these two methods on a large dataset of 10,000 objects each. **Options Compared** The benchmark compares two options: 1. **Lodash Intersection**: Using the `_.intersection` function from the Lodash library to intersect the two arrays. 2. **Set-based Intersect**: Using Sets to create two sets from the input arrays and then filtering one set based on the existence of elements in the other set. **Pros and Cons** * **Lodash Intersection**: + Pros: Convenient, readable, and often used in larger applications. + Cons: May have overhead due to its library dependencies, and can be slower than raw Set operations for very large datasets. * **Set-based Intersect**: + Pros: Can be faster for large datasets, as it leverages the optimized `has` method of Sets. Also, less dependent on external libraries. + Cons: May require more setup and configuration, as it involves creating two sets and filtering one based on the other. **Library Used** * **Lodash**: A popular utility library that provides a wide range of functions for tasks like array manipulation, string manipulation, and more. In this case, `_.intersection` is used to find the intersection of two arrays. **Special JS Feature or Syntax** None mentioned in the provided JSON, but it's worth noting that if we had custom JavaScript features or syntax being tested, it would be essential to consider how those might impact performance. **Other Alternatives** * **Using `filter()` with `includes()`**: Another approach could involve using the `includes()` method on one of the arrays and filtering the other array based on this. This might have similar performance characteristics to Set-based Intersect. * **Using `reduce()`**: You could also consider using a reduction function like `_.reduce` from Lodash or the built-in `Array.prototype.reduce()` method to find the intersection. **Benchmark Preparation Code Explanation** The script preparation code creates two empty arrays, `first` and `second`, each with 10,000 elements. The `batchId` and `userId` properties are randomly generated for each element using `Math.random()`. This dataset is likely intended to be representative of real-world data. **Benchmark Results Interpretation** The latest benchmark results show the execution rate (Executions Per Second) for each test case on a Chrome 100 browser running on Linux. The first result indicates that the Lodash Intersection approach takes around 354 executions per second, while the Set-based Intersect approach is slightly faster at approximately 294 executions per second. This suggests that using Sets with filtering might be a more efficient approach for this specific use case. However, it's essential to note that performance differences can vary depending on the specific dataset and hardware configurations being tested.
Related benchmarks:
Array Intersection vs. Set Intersection vs. Lodash part 3
loadash vs set
loadash vs set eg2
Array Intersection vs. Set Intersection vs. Lodash - big
Comments
Confirm delete:
Do you really want to delete benchmark?