Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash intersectionBy vs array filtering with includes
(version: 0)
Return only objects with same id
Comparing performance of:
lodash intersectionBy vs array filtering with includes
Created:
5 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 intersectionBy
_.intersectionBy(listOfObjects_1, listOfObjects_2, '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 intersectionBy
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 intersectionBy
852457.6 Ops/sec
array filtering with includes
14334470.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested. **Benchmark Definition** The benchmark definition specifies two test cases: 1. `_.intersectionBy(listOfObjects_1, listOfObjects_2, 'id')`: This test case uses the `lodash` library to perform an intersection by key on two arrays of objects (`listOfObjects_1` and `listOfObjects_2`) based on the `'id'` key. 2. `var ids_1 = listOfObjects_1.map(o => o.id)\nlistOfObjects_2.filter(o => ids_1.includes(o.id))`: This test case manually implements an array filtering with includes method using JavaScript's built-in `map` and `filter` functions. **Options Compared** The two options being compared are: 1. Using the `lodash` library's `intersectionBy` function. 2. Implementing the intersection by key manually using JavaScript's built-in `map` and `filter` functions. **Pros and Cons of Each Approach** **Lodash `intersectionBy` Function:** Pros: * Convenient and concise way to perform intersections by key on arrays of objects. * Often more readable and maintainable than manual implementations. Cons: * Introduces an additional dependency on the `lodash` library. * May have performance overhead due to the need to load the library. **Manual Implementation using `map` and `filter`:** Pros: * No additional dependencies or overhead. * Can be more flexible and customizable for specific use cases. Cons: * Often more verbose and less readable than using a library function. * Requires manual error handling and edge case consideration. **Library: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, object transformation, and functional programming. The `intersectionBy` function is designed to find the common elements between two arrays of objects based on a specified key. **Special JS Feature/Syntax: None** There are no special JavaScript features or syntax used in this benchmark that would require additional explanation. **Other Alternatives** If you wanted to implement an intersection by key manually without using `lodash`, you could use other libraries like `underscore` or implement it yourself using a custom function. Alternatively, you could also consider using other built-in functions like `Set` and `forEach` to achieve the same result. It's worth noting that both approaches have their trade-offs in terms of performance, readability, and maintainability. The choice between them ultimately depends on your specific use case, personal preference, and project requirements.
Related benchmarks:
Array.prototype.filter vs Lodash filter
native intersect vs lodash intersection
lodash intersectionWith vs array filtering with includes
Array.prototype.filter vs Lodash without 2
Comments
Confirm delete:
Do you really want to delete benchmark?