Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find() vs for...of vs for-loop large array fixed
(version: 0)
Testing the difference between native loops and find()
Comparing performance of:
for-loop vs for..of vs Array.find()
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id='test'></div>
Script Preparation code:
window.arr = Array.from({ length: 1000 }, (_, i) => i)
Tests:
for-loop
let val; for(i=0; i<arr.length; i++){ var value = arr[i]; if (value === 900) { val = value; break; } }
for..of
let val; for (var value of arr) { if (value === 900) { val = value; break; } }
Array.find()
let val = arr.find(node => node === 900);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for-loop
for..of
Array.find()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0
Browser/OS:
Firefox 123 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-loop
60038.1 Ops/sec
for..of
175157.3 Ops/sec
Array.find()
374247.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark that compares three different approaches for finding an element in a large array: 1. **Native Loop**: A traditional `for` loop using the `length` property to iterate over the array. 2. **`for...of` Loop**: An iteration method introduced in ECMAScript 2015, which uses the `for...of` statement to loop over iterable objects, such as arrays. 3. **`Array.find()` Method**: A built-in method on the `Array` prototype that searches for an element in the array. **Test Case Explanation** The individual test cases are defined within a nested JSON structure. Each test case has: * **Benchmark Definition**: The code snippet that represents the benchmark being tested. * **Test Name**: A brief description of the benchmark. * **Script Preparation Code** and **Html Preparation Code**: Optional code snippets used to set up the environment for each test. The three test cases are: 1. `for-loop`: A traditional `for` loop using the `length` property to iterate over the array. 2. `for..of`: An iteration method introduced in ECMAScript 2015, which uses the `for...of` statement to loop over iterable objects, such as arrays. 3. `Array.find()`: The built-in `Array.find()` method that searches for an element in the array. **Pros and Cons of Each Approach** Here's a brief analysis of each approach: 1. **Native Loop (`for-loop`)**: * Pros: Typically faster due to low-level memory management and iteration control. * Cons: Requires manual indexing and bounds checking, which can be error-prone. 2. **`for...of` Loop**: * Pros: Easier to read and maintain than traditional `for` loops, with reduced risk of off-by-one errors. * Cons: May be slower due to the overhead of creating an iteration context. 3. **`Array.find()` Method**: * Pros: Concise and expressive code, with built-in support for searching large arrays. * Cons: May not perform as well on very large arrays or memory-constrained environments. **Library Used (if any)** None of the provided benchmark definitions rely on a specific library. The `Array.from()` method is used to create a large array in the script preparation code, but this is a built-in JavaScript function. **Special JS Feature/Syntax** The only special feature used in these benchmarks is the introduction of the `for...of` iteration method in ECMAScript 2015. This syntax allows for more expressive and concise code when iterating over iterable objects like arrays. **Other Alternatives** Alternative approaches for finding an element in a large array might include: * Using `Array.prototype.indexOf()` or `Array.prototype.lastIndexOf()`, which return the index of the first/last occurrence, respectively. * Employing custom linear search algorithms, such as binary search, to improve performance on very large arrays. Keep in mind that these alternatives may have different trade-offs in terms of code readability, performance, and memory usage.
Related benchmarks:
foreach vs for of
foreach vs for..of
is find faster than forEach?
Get index with forEach vs for...of over entries
foreach vs for...of
Comments
Confirm delete:
Do you really want to delete benchmark?