Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
custom Set vs Lodash vs custom Array intersection (v3)
(version: 3)
Comparing performance of:
Custom Set intersection vs Lodash intersection vs custom Array intersection
Created:
4 years ago
by:
Registered User
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 = [4, 5, 6, 7, 8]; var second = [1, 2, 3, 4]; var firstSet = new Set(first) var secondSet = new Set(second);
Tests:
Custom Set intersection
function hasIntersection(set, iterable) { if (set.size > 0) { for (const item of iterable) { if (set.has(item)) { return true; } } } return false; } function doSetsHaveIntersection(a, b) { if (a.size > b.size) { return hasIntersection(a, b); } else { return hasIntersection(b, a); } } doSetsHaveIntersection(secondSet, firstSet)
Lodash intersection
_.intersection(second, first)
custom Array intersection
function doArraysIntersect( firstArray, secondArray ) { if (!firstArray || firstArray.length === 0 || !secondArray || secondArray.length === 0) { return false; } for (let ii = 0; ii < firstArray.length; ii++) { const element = firstArray[ii]; if (secondArray.indexOf(element) > -1) { return true; } } return false; } doArraysIntersect(second, first)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Custom Set intersection
Lodash intersection
custom Array intersection
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 compares three approaches to check if two sets or arrays have an intersection: 1. Custom Set Intersection (using `Set` objects) 2. Lodash Intersection (using the `intersection` function from the Lodash library) 3. Custom Array Intersection (a simple implementation using array iteration) **Options Compared** The benchmark is comparing the performance of these three approaches across multiple executions. **Pros and Cons of Each Approach** 1. **Custom Set Intersection**: * Pros: Simple to implement, efficient for large sets. * Cons: Requires creating `Set` objects, which can be slow for small inputs due to hashing overhead. 2. **Lodash Intersection**: * Pros: Convenient, well-tested library function that handles edge cases. * Cons: Adds external dependency (Lodash), may be slower than custom implementations due to indirect function calls. 3. **Custom Array Intersection**: * Pros: Simple and lightweight implementation using array iteration. * Cons: May be slower for large inputs due to the overhead of iterating over arrays. **Library Used** The Lodash library is used in this benchmark, specifically the `intersection` function. This function takes two iterables (sets or arrays) as input and returns a new iterable containing only the elements that are present in both inputs. **Special JS Feature/Syntax** None mentioned. **Alternative Approaches** Other possible approaches to check for set intersection could include: * Using `Array.prototype.filter()` with a callback function * Implementing a custom iterative algorithm using bit manipulation (e.g., bitwise AND) * Utilizing parallel processing or multi-threading to speed up the computation However, these alternatives are not being tested in this benchmark. **Benchmark Preparation Code** The preparation code sets up two arrays `first` and `second`, which will be used as inputs for the intersection checks. The script also creates `Set` objects from these arrays (`firstSet` and `secondSet`) using the custom set intersection approach. **Latest Benchmark Result** The results show that the Custom Array Intersection approach is significantly faster than the other two approaches, likely due to its lightweight implementation and lack of overhead from external dependencies or indirect function calls.
Related benchmarks:
Array Intersection vs. Set Intersection vs. Lodash
Array Intersection vs. Set Intersection vs. Lodash part 3
Array Intersection vs. Set Intersection vs. Lodash - big
Array vs. Set Intersection #2
Comments
Confirm delete:
Do you really want to delete benchmark?