Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash intersectionWith vs array filtering with includes
(version: 0)
Comparing performance of:
lodash intersectionWith vs array filtering with includes
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="lodash.js"></script>
Script Preparation code:
var listOfObjects_1 = [{id: 1, name: '1'}, {id: 2, name: '2'}, {id: 5, name: '5'}] var listOfObjects_2 = [{id: 1, name: '1'}, {id: 2, name: '2'}, {id: 5, name: '4'}]
Tests:
lodash intersectionWith
_.intersectionWith(listOfObjects_1, listOfObjects_2, (a,b) => a.id === b.id)
array filtering with includes
var ids_1 = listOfObjects_1.map(o => o.id) listOfObjects_2.filter(o => ids_1.includes(o.id))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash intersectionWith
array filtering with includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash intersectionWith
2820833.2 Ops/sec
array filtering with includes
14458336.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **What is being tested?** The provided JSON represents two benchmark test cases: 1. **Lodash `intersectionWith`**: This test case uses the Lodash library to find the intersection between two arrays, `listOfObjects_1` and `listOfObjects_2`, based on a custom comparison function `(a,b) => a.id === b.id`. The goal is to measure how efficient this implementation is compared to another approach. 2. **Array filtering with `includes`**: This test case uses native JavaScript methods to find the intersection between two arrays, `listOfObjects_1` and `listOfObjects_2`, by mapping `listOfObjects_1` to an array of IDs (`ids_1`) and then filtering `listOfObjects_2` using `includes()` to check if each ID is present. **Options compared** The two test cases compare the following options: * **Lodash `intersectionWith`**: A library-based implementation that uses a custom comparison function. * **Array filtering with `includes`**: A native JavaScript implementation that relies on built-in array methods. **Pros and Cons of each approach:** 1. **Lodash `intersectionWith`**: * Pros: + Abstracts away the complexity of implementing intersection logic, making it easier to write efficient code. + Can be more concise and readable, especially for complex comparison functions. * Cons: + Introduces additional overhead due to the library itself and its custom implementation. + May not be as optimized for performance as native JavaScript implementations. 2. **Array filtering with `includes`**: * Pros: + No additional overhead beyond what's required by the language itself. + Can be highly optimized for performance, especially when using native array methods. * Cons: + Requires more boilerplate code to implement correctly. + May not be as readable or maintainable due to its complexity. **Library used:** Lodash is a popular JavaScript utility library that provides various functions for tasks like array manipulation, object manipulation, and functional programming. In this case, the `intersectionWith()` function is used to find the intersection between two arrays based on a custom comparison function. **Special JS feature or syntax:** There isn't any special JavaScript feature or syntax being tested in these benchmark test cases. The focus is on comparing the performance of different approaches to implementing array intersection logic. **Other alternatives:** If you're interested in exploring other alternatives for implementing array intersection logic, here are a few options: * **Using `filter()` and `some()`**: You can use `filter()` to create a new array containing only the elements that pass a test implemented by a provided function (like `some()`), which returns a boolean value indicating whether an element passes the test. * **Using `reduce()` and `every()`**: You can use `reduce()` to iterate over one of the arrays, comparing each element with the corresponding element in the other array using `every()`, which returns a boolean value indicating whether all elements pass the comparison. These alternative approaches may have different performance characteristics than Lodash's `intersectionWith()` or array filtering with `includes()`.
Related benchmarks:
Array.prototype.filter vs Lodash filter
lodash intersectionBy vs array filtering with includes
native intersect vs lodash intersection
Array.prototype.filter vs Lodash without 2
Comments
Confirm delete:
Do you really want to delete benchmark?