Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find faster - for vs find vs map vs foreach II
(version: 0)
Comparing performance of:
forEach vs map vs find and foreach vs nested foreach and filter vs nested filter and map vs nested filter and reduce
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [1,1,1,2,3,4,5,6,7,7,7,7,8,9,10,12,131,124,123,1525,1563,14234,123123,4,7,0,6,2,1,5,44,66,778,888,56,7,5,3,21,2,19]
Tests:
forEach
let foundForEachIndex = [] arr.forEach(function (item){ if (item[i] == 7){ foundForEachIndex.push(i) return } })
map
let foundMapIndex = arr.map(item => { if (item[i] == 7){ return i } })
find and foreach
const found = arr.find(function(element) { return element == 7 }) if(found > -1){ let foundForEachIndex = [] arr.forEach(function (item){ if (item[i] == 7){ foundForEachIndex.push(i) } }) }
nested foreach and filter
let foundForEachIndex = [] arr.filter( e => e == 7).forEach(function (item){ foundForEachIndex.push(i) return })
nested filter and map
arr.filter( e => e == 7).map(function (item){ return item })
nested filter and reduce
arr.filter(e => e==7).reduce((sum, e) => sum + e)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
forEach
map
find and foreach
nested foreach and filter
nested filter and map
nested filter and reduce
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
gemma2:9b
, generated one year ago):
This benchmark tests different methods for finding the indices of all occurrences of the number `7` in a given array. Let's break down each option: **1. `forEach`:** This approach iterates through every element in the array using the `forEach` loop and checks if the current element is equal to `7`. If it is, the index is pushed into an array (`foundForEachIndex`). * **Pros:** Simple and straightforward implementation. * **Cons:** Can be inefficient as it iterates through the entire array even after finding a match. It also requires extra memory for storing the indices in `foundForEachIndex`. **2. `map`:** This approach uses the `map` function to transform each element in the array. For each element, if it's equal to `7`, its index is returned. * **Pros:** More concise than `forEach`. * **Cons:** Still iterates through the entire array. If you only need the indices of occurrences of `7`, creating an array with all indices might be unnecessary. **3. `find and foreach`:** This approach first uses `find` to locate the index of the first occurrence of `7`. Then, it iterates through the array using `forEach` again to find all other occurrences. * **Pros:** Potentially faster than `forEach` or `map` if you only need the indices of a few occurrences of `7`. * **Cons:** Redundant iteration after finding the first occurrence. **4. `nested foreach and filter`:** This approach uses `filter` to create a new array containing only the elements equal to `7`, then iterates through this filtered array using `forEach` to find the indices. * **Pros:** More efficient than previous approaches if you need all indices of `7`. * **Cons:** Still requires an extra array for filtering. **5. `nested filter and map`:** This approach filters the array for elements equal to `7`, then uses `map` to get the indices of those filtered elements. * **Pros:** More concise than `nested foreach and filter`. * **Cons:** Still requires an extra array for filtering. **Benchmark Results:** The results show that `nested filter and map` is generally the fastest method, followed by `nested foreach and filter`. The `forEach` and `find and foreach` approaches are slower because they involve redundant iterations. The `map` approach is in the middle due to its conciseness but still requiring iteration through the entire array. **Alternatives:** * **`findIndex`:** This method could be used with `filter` for a more efficient solution. * **Regular Expressions (for specific cases):** If you're dealing with strings containing numbers, regular expressions can be faster for finding matches.
Related benchmarks:
is find faster than forEach?
Array.forEach vs Object.keys().forEach
Array loop vs foreach vs map fixed by bomi
Array fill map, vs for i loop
Comments
Confirm delete:
Do you really want to delete benchmark?