Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array ForEach vs Find vs for loop (2)
(version: 0)
Comparing performance of:
ForEach vs Find vs For
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 10000; i++) { arr[i] = i; } var needToFindThis = Math.floor((Math.random() * 10000) + 1);
Tests:
ForEach
var indexFound = -1; arr.forEach((item, index) => { if (item === needToFindThis) { indexFound = index; } });
Find
var indexFound = arr.find((item, index) => { if (item === needToFindThis) { return index; } });
For
var indexFound = -1; for (var i = 0; i < arr.length; i++) { if (arr[i] === needToFindThis) { indexFound = i; break; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
ForEach
Find
For
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
ForEach
2889.5 Ops/sec
Find
23684.5 Ops/sec
For
8698.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Overview** The benchmark is testing three different approaches to find an element in an array: 1. `Array.prototype.forEach()` 2. `Array.prototype.find()` 3. A traditional `for` loop **Library Used** None specific libraries are used in this benchmark. However, the `find()` method relies on a library called "Lodash" which is not included in this JSON. **JavaScript Features/Syntax** The benchmark uses JavaScript's built-in `forEach()` and `find()` methods, as well as a traditional `for` loop with the `break` statement. Here's a brief explanation of each approach: 1. **Array.prototype.forEach()**: This method iterates over an array using a callback function, allowing you to perform actions on each element. In this case, it's used to find an element in the array by comparing it with the target value. 2. **Array.prototype.find()**: This method returns the first element in the array that satisfies a provided testing function (also known as the "predicate"). If no elements satisfy the testing function, `find()` returns `undefined`. In this case, it's used to find an element by comparing it with the target value and returning its index. 3. **Traditional For Loop**: This is a basic `for` loop that iterates over an array using an index variable. When the condition is met, the `break` statement exits the loop. **Pros and Cons** 1. **Array.prototype.forEach()**: * Pros: Easy to use, concise syntax. * Cons: Can be slower than traditional loops, as it creates a new scope for each iteration. 2. **Array.prototype.find()**: * Pros: Convenient when you only need to find one element, relatively fast. * Cons: Returns `undefined` if no elements match, and can be slower for large arrays. 3. **Traditional For Loop**: * Pros: Can be faster than `forEach()` or `find()` methods, as it avoids the overhead of creating a new scope. * Cons: More verbose syntax, requires manual index management. **Alternative Approaches** Other approaches to find an element in an array include: 1. Using `Array.prototype.includes()`: This method is more concise and can be faster than traditional loops. 2. Using `Map` or `Set` data structures: These provide efficient lookups for elements in the collection. In general, the choice of approach depends on the specific requirements of your application, such as performance, readability, and maintainability.
Related benchmarks:
map vs forEach vs for loop
Fill array with random integers
Preinitialized array size vs Push operations to an empty one.
Array.reduce vs for loops vs Array.forEach
for of vs Array.reduce vs Array.forEach vs for i for summing and array of integers
Comments
Confirm delete:
Do you really want to delete benchmark?