Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object arrays: find vs for loop
(version: 0)
Testing finding an object array via find or by using a for loop
Comparing performance of:
find vs for loop
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(15000); arr.fill({ id: 0 }); arr = arr.map((el, idx) => el.id = idx); var foo = Math.floor(Math.random() * 15000);
Tests:
find
arr.find((itm) => itm.id === foo);
for loop
for (let i = 0; i < arr.length; i++) { if (arr[i].id === foo) break; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
find
for loop
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 benchmark and explain what's being tested. **Benchmark Definition** The benchmark is testing two approaches to find an object in an array: 1. **`arr.find((itm) => itm.id === foo)`**: This uses the `find()` method, which returns the first element in the array that satisfies the provided condition. 2. **`for (let i = 0; i < arr.length; i++) {\r\n if (arr[i].id === foo) break;\r\n}`**: This uses a traditional `for` loop to iterate through the array and find the first element that matches the condition. **Options being compared** The two options being compared are: * Using the `find()` method * Using a traditional `for` loop **Pros and Cons of each approach** 1. **`arr.find((itm) => itm.id === foo)`** * Pros: + More concise and expressive code + Can be more efficient for large arrays, as it stops iterating as soon as the condition is met * Cons: + May have a higher overhead due to the function invocation and call stack management + Not all browsers support `find()` (although most modern ones do) 2. **`for (let i = 0; i < arr.length; i++) {\r\n if (arr[i].id === foo) break;\r\n}`** * Pros: + Widely supported in all browsers + Can be more intuitive for developers familiar with traditional loops * Cons: + More verbose and less concise code + May have a higher overhead due to the loop iteration **Other considerations** * **Array size**: The benchmark uses an array of 15,000 elements, which is a common choice for demonstrating performance differences between these two approaches. * **Randomized search**: The `foo` variable is set to a random index in the array, making it more likely that the condition will be met quickly. **Library or special JS feature** Neither of the benchmark options explicitly uses any external libraries. However, the use of the `find()` method relies on the ECMAScript 5 standard, which introduced this functionality. **Special JS feature (not applicable in this case)** None of the benchmark options explicitly use any special JavaScript features like async/await, generators, or arrow functions (although the `map` function does). **Other alternatives** If you wanted to test these approaches using different methods, here are some alternatives: * Use a `while` loop instead of a traditional `for` loop. * Use `every()` and `some()` methods to find all matching elements instead of just the first one. * Compare performance with other array methods like `forEach()`, `reduce()`, or `filter()`. * Test with arrays of different sizes, shapes, or data types. I hope this explanation helps you understand the benchmark and its underlying assumptions!
Related benchmarks:
Object arrays: findIndex vs for loop
Object arrays: findIndex vs for loop2
Object arrays: findIndex vs for loop (length cached)
Object arrays: findIndex vs for loop vs some
Comments
Confirm delete:
Do you really want to delete benchmark?