Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
foreach vs filter+map
(version: 0)
Comparing performance of:
filter + map vs forEach
Created:
8 years ago
by:
Guest
Jump to the latest result
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:
filter + map
selectedItems = Object.values(contacts).map(contact => contact.id).filter(id => selectedItems.indexOf(id) === -1)
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
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 JSON data to explain what is being tested, the different approaches compared, and their pros and cons. **Benchmark Definition** The benchmark definition compares two approaches: `forEach` vs `filter + map`. The script preparation code creates an object called `contacts` with 10 properties, each representing a contact with an ID. A second array called `selectedItems` is created with three IDs that should be filtered out from the contacts. **Approaches Compared** 1. **forEach**: This approach iterates over the `Object.values(contacts)` and checks if the current ID is in the `selectedItems` array using `indexOf()`. If not, it adds the ID to the `selectedItems` array. 2. **filter + map**: This approach uses the `map()` function to create a new array with IDs from contacts that are not in the `selectedItems` array. The `filter()` function is then used to remove duplicates from the resulting array. **Pros and Cons** 1. **forEach**: * Pros: + Iterates over the objects directly, avoiding unnecessary array operations. + Does not require multiple iterations (map + filter). * Cons: + May be slower due to the additional iteration using `indexOf()`. + Less efficient in terms of memory usage, as it modifies the original array with push(). 2. **filter + map**: * Pros: + Faster and more efficient in terms of memory usage, as it avoids modifying the original array. + Can be faster due to fewer iterations (map + filter). * Cons: + Requires creating intermediate arrays (using `map()`), which can increase memory allocation time. + More complex and harder to read for some developers. **Libraries Used** In this benchmark, the `Object.values()` function is used from the ECMAScript Standard Library. This function returns an array of values that would be obtained by using the Object.entries() method in a for...of loop or the Object.keys() method with the spread operator. The `indexOf()` function is also part of the ECMAScript Standard Library, which searches for the first occurrence of a specified element within a given array. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark. The code uses standard JavaScript functions and methods, making it accessible to developers familiar with the language. **Alternatives** Other approaches that could be considered as alternatives include: * Using `Array.prototype.some()` instead of `indexOf()`. * Using `Array.prototype.findIndex()` if you want to get the index of the first non-matching element (instead of using `-1`). * Using a more modern approach, such as using `Array.prototype.filter()` with an initial value. * Avoiding array modifications by creating a new array and assigning it back to the original variable. However, considering the simplicity and readability of the code, these alternatives may not be significantly better or worse than the two approaches being compared.
Related benchmarks:
foreach vs filter+map
foreach vs filter+map
Lodash filter + map vs forEach
Lodash filter + map vs forEach - corrected
Comments
Confirm delete:
Do you really want to delete benchmark?