Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Testing loops and finds
(version: 0)
Comparing performance of:
for loop vs for of vs array find
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.arr = new Array(100000).fill().map((x, i) => Math.random()*100000); window.target = arr[50000];
Tests:
for loop
for(i=0; i<window.arr.length; i++){ var value = arr[i]; if (value === window.target) { val = value; break; } }for(i=0; i<window.arr.length; i++){ var value = arr[i]; if (value === window.target) { val = value; break; } }for(i=0; i<window.arr.length; i++){ var value = arr[i]; if (value === window.target) { val = value; break; } }
for of
let val; for (var value of window.arr) { if (value === window.target) { val = value; break; } }
array find
let val = window.arr.find(val => val === window.target);
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:
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):
I'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The provided benchmark tests three different approaches to find an element in an array: 1. **For loop**: Uses a traditional `for` loop to iterate through the array. 2. **For of**: Utilizes the `for...of` loop syntax to iterate through the array. 3. **Array find**: Leverages the `find()` method to search for the target value. **Script Preparation Code** The script preparation code sets up an array `arr` with 100,000 elements, each filled with a random number between 0 and 100,000. It also defines a `target` variable as the middle element of the array (`arr[50000]`). **Test Cases** There are three test cases: 1. **For loop**: The benchmark executes a traditional `for` loop twice to find the target value. 2. **For of**: The benchmark uses the `for...of` loop syntax to iterate through the array and find the target value. 3. **Array find**: The benchmark uses the `find()` method to search for the target value in the array. **Comparison** The test cases are compared based on their execution speed, measured in executions per second (`ExecutionsPerSecond`). The browser, device platform, operating system, and raw UA string (User Agent) are also reported. **Pros and Cons of Each Approach** 1. **For loop**: Traditional `for` loops can be more predictable and easier to optimize for performance, but they can also lead to slower execution times due to the overhead of incrementing the index variable. 2. **For of**: The `for...of` loop syntax is a modern and concise way to iterate through arrays, but it may not provide direct access to the index variable, which can make optimization more challenging. However, `for...of` loops are generally faster than traditional `for` loops due to better compiler optimizations. 3. **Array find**: The `find()` method is a convenient and efficient way to search for an element in an array, but it may incur additional overhead due to the creation of an intermediate iterator object. **Library Usage** None of the test cases use any external libraries beyond the built-in JavaScript features. **Special JS Features or Syntax** The benchmark uses the following modern JavaScript feature: * `for...of` loop syntax (introduced in ECMAScript 2015) * `find()` method (introduced in ECMAScript 2015) **Other Alternatives** Some alternative approaches to find an element in an array include: * Using a linear search algorithm, which involves checking each element individually. * Utilizing a binary search algorithm, which takes advantage of the array's sorted nature to reduce the number of comparisons required. However, these alternatives are typically less efficient than the `find()` method and may not provide better performance for large arrays.
Related benchmarks:
Fill array with random integers
array pre alloc n
Sparse array vs Map for random access and iteration
Object vs Map lookup: random integer key
Fill an MxN 2D nested array with random numbers
Comments
Confirm delete:
Do you really want to delete benchmark?