Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.find() vs for loop
(version: 0)
Comparing performance of:
Array.find vs For-loop
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const arr = Array.from({ length: 20000 }, (_, i) => ({ property: i })) const elementToFind = 15000;
Tests:
Array.find
const arr = Array.from({ length: 20000 }, (_, i) => ({ property: i })) const elementToFind = 15000; const element = arr.find((el) => el.property === elementToFind);
For-loop
const arr = Array.from({ length: 20000 }, (_, i) => ({ property: i })) const elementToFind = 15000; let element = undefined; for (let i = 0; i < arr.length; i++) { if (arr[i].property === elementToFind) { element = arr[i]; break; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.find
For-loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.find
1841.9 Ops/sec
For-loop
2140.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is comparing the performance of `Array.find()` with a traditional for loop in JavaScript. The test creates an array of 20,000 elements and searches for a specific element (index 15,000) using both methods. **Options Compared** 1. **`Array.find()`**: This method returns the first element in the array that satisfies the provided condition (in this case, `el.property === elementToFind`). If no such element is found, it returns `undefined`. 2. **Traditional For Loop**: The for loop iterates through each element of the array, checking if its `property` matches the target value. As soon as a match is found, the loop breaks and assigns the matched element to the `element` variable. **Pros and Cons** * **`Array.find()`**: + Pros: concise, readable, and efficient for most use cases. + Cons: may not be suitable for large arrays or performance-critical applications due to its overhead. In this case, it's worth noting that `Array.from()` is used to create the array, which can add additional overhead. * **Traditional For Loop**: + Pros: lightweight, easy to understand, and suitable for large arrays or performance-critical applications. + Cons: can be more verbose and harder to read than `Array.find()`. Additionally, it requires manual index management. **Library Used** There is no explicit library mentioned in the benchmark definition. However, it's worth noting that `Array.from()` is a standard JavaScript method for creating an array from an iterable. **Special JS Feature or Syntax** This benchmark does not use any special JavaScript features or syntax beyond what's already mentioned (e.g., arrow functions, template literals). If you're interested in exploring other features, consider looking into other benchmark tests. **Other Alternatives** If you want to explore alternative methods for searching arrays, here are a few options: * **`Array.prototype.findIndex()`**: Similar to `Array.find()`, but returns the index of the first element that matches the condition (or -1 if no match is found). * **`Array.prototype.map()`** and **`reduce()`**: While not exactly designed for searching, these methods can be used with a callback function to find elements in an array. * **Native WebAssembly (WASM) Arrays**: If you're interested in exploring performance-critical applications on WASM, consider using native WASM arrays or libraries like `wasm-array-search`. Keep in mind that the best approach will depend on your specific use case and requirements.
Related benchmarks:
find vs findIndex (Array prototype methods)
find vs findindex with condition test
find vs findIndex (Array prototype methods) stop at first found
find() vs indexOf() vs for...of vs for-loop - bigger array
find vs indexOf (Array prototype methods)
Comments
Confirm delete:
Do you really want to delete benchmark?