Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash filter + map vs forEach
(version: 0)
Comparing performance of:
Lodash filter + map vs forEach
Created:
7 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 contacts = { 'a1@a.com': {id: 1}, 'a2@a.com': {id: 2}, 'a3@a.com': {id: 3}, 'a4@a.com': {id: 4}, 'a5@a.com': {id: 5}, 'a6@a.com': {id: 6}, 'a7@a.com': {id: 7}, 'a8@a.com': {id: 8}, 'a9@a.com': {id: 9}, 'a10@a.com': {id: 10}, } var selectedItems = [1,3,7];
Tests:
Lodash filter + map
selectedItems = _Object.values(contacts).filter(contact => selectedItems.indexOf(contact.id) === -1).map(contact => contact.id)
forEach
Object.values(contacts).forEach((contact) => { if (selectedItems.indexOf(contact.id) === -1) { selectedItems.push(contact.id); } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash filter + map
forEach
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 definition and individual test cases. **Benchmark Definition:** The website `MeasureThat.net` is used to create and run JavaScript microbenchmarks. The provided benchmark measures the performance of two approaches for filtering and mapping an array of objects: 1. Using `filter()` and `map()` from Lodash library. 2. Using a simple `forEach()` loop with conditional logic. **Options Compared:** The two options compared are: * **Lodash filter + map**: This approach uses the `filter()` method to remove elements that don't match the condition, followed by the `map()` method to transform the remaining elements into an array of IDs. * **forEach**: This approach uses a simple `forEach()` loop with conditional logic to push the ID of each element into the `selectedItems` array if it's not already present. **Pros and Cons:** Both approaches have their strengths and weaknesses: * **Lodash filter + map**: + Pros: - More concise and readable code. - Takes advantage of optimized Lodash functions. + Cons: - Requires an additional library (Lodash) to be included in the test environment. - Might have slower startup times due to the import time. * **forEach**: + Pros: - No additional libraries needed, as it's a built-in method. - Can be more familiar and comfortable for developers who are used to traditional loops. + Cons: - Code might be longer and less readable due to the nested if statement. - The loop might not be optimized as much as the Lodash approach. **Library:** The Lodash library is a popular utility library that provides various functions for tasks like filtering, mapping, and more. In this case, it's used to simplify the code and take advantage of optimized implementations. **Special JS Feature or Syntax:** There isn't any special JavaScript feature or syntax mentioned in the benchmark definition. Both approaches use standard JavaScript methods and syntax. **Other Alternatives:** If you want to explore other alternatives for filtering and mapping arrays, here are a few options: * **Array.prototype.filter()**: A built-in method that can be used without an additional library. * **Array.prototype.reduce()**: Another built-in method that can be used for both filtering and mapping arrays, although it's often more complex than the simple `forEach()` approach. * **Underscore.js functions**: Similar to Lodash, but with a slightly different API.
Related benchmarks:
lodash _.map vs native map
lodash _.map vs native map true version
uniqBy performance and map
test native vs lodash 1
Array Map Vs Lodash Map (1)
Comments
Confirm delete:
Do you really want to delete benchmark?