Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
intersectionWith vs find in find vs set small
(version: 0)
Comparing performance of:
intersectionWith vs find in find vs set
Created:
3 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 a = Array.from({length: 100}, (_, i) => [i]) var b = Array.from({length: 30}, (_, i) => [i * 5 + 50])
Tests:
intersectionWith
!!_.intersectionWith(a, b, (a, b) => a[0] === b[0]).length
find in find
a.find(aIt => b.find(bIt => a[0] === b[0]))
set
const set = new Set(b) a.find(it => set.has(it[0]))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
intersectionWith
find in find
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):
**Overview** The provided JSON represents a benchmark test on MeasureThat.net, which compares the performance of three approaches to find intersections between two arrays: `intersectionWith`, `find in find`, and using a `Set` data structure. **Benchmark Test Cases** 1. **`intersectionWith`**: This approach uses the `lodash.intersectionWith` function to compare each element of both arrays and returns an array with only the common elements. 2. **`find in find`**: This approach uses two nested `find` methods to search for common elements between the two arrays. It first finds an element in the first array that matches an element in the second array, and then finds another element in the first array that also matches the same element in the second array. 3. **`set`**: This approach creates a `Set` data structure from the second array and then uses a single `find` method to search for elements in the first array that exist in the set. **Comparison of Approaches** * **Efficiency**: The `intersectionWith` approach is likely to be the most efficient, as it directly compares each element between the two arrays using a custom comparison function. In contrast, the `find in find` approach involves nested method calls and may incur additional overhead due to repeated traversals of the array. * **Readability**: The `set` approach might seem less readable at first glance, but creating a set from the second array allows for a simple and efficient search using the `has` method. This can be beneficial if the size of the second array is large or known in advance. **Pros and Cons** * **`intersectionWith`**: * Pros: Direct comparison of elements, potentially more efficient. * Cons: Requires custom comparison function, might not work with all data types. * **`find in find`**: * Pros: Uses built-in `find` method, can be easily extended to support additional logic. * Cons: Involves nested method calls, potentially slower due to repeated array traversals. * **`set`**: * Pros: Can be efficient for large arrays or known sizes, uses a simple and fast search method. * Cons: Requires creating an extra data structure (the set), might incur additional memory usage. **Library and Special JS Features** * The `lodash.intersectionWith` function is used in the `intersectionWith` approach. Lodash is a popular JavaScript utility library that provides various functions for tasks like array manipulation, string manipulation, and more. * No special JS features or syntax are mentioned in the provided benchmark code. **Alternatives** If you were to rewrite this benchmark test using alternative approaches: * You could use `Array.prototype.filter` instead of `intersectionWith`. This would involve creating a new filtered array with common elements and might incur additional overhead. * Instead of `set`, you could use `Array.prototype.includes` or a simple for loop-based approach, which would be less efficient but easier to understand. Keep in mind that the best approach depends on your specific requirements, such as performance needs versus code readability.
Related benchmarks:
native intersect vs lodash intersection
lodash intersection vs JS
lodash intersectionWith vs array filtering with includes
intersectionWith vs find in find vs set big
Comments
Confirm delete:
Do you really want to delete benchmark?