Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find vs for of
(version: 0)
Measuring which is faster
Comparing performance of:
for of vs Array.prototype.findIndex
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 1E5) arr[i] = i++;
Tests:
for of
const item = arr.find(item => item == 1E5);
Array.prototype.findIndex
let item for ( const i of arr ) { if( i == 1E5 ) { item = i return } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for of
Array.prototype.findIndex
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 provided JSON and explain what's being tested, compared options, pros/cons, and other considerations. **What is being tested?** The test case measures the performance difference between two approaches to find an element in an array: 1. Using `Array.prototype.findIndex()` method 2. Using a traditional `for` loop with `indexOf()` 3. Using the `for...of` loop **Script Preparation Code** The script preparation code creates an empty array `arr` and populates it with numbers from 0 to 100,000. ```javascript var arr = []; var i = 0; while (i <= 1E5) { arr[i] = i++; } ``` This code generates a large array of numbers, which is likely used as the input for the benchmark. **Html Preparation Code** The html preparation code is empty (`null`), indicating that no HTML-related setup or configuration is needed for this benchmark. **Test Cases** There are two individual test cases: 1. **"for of"`**: This test case uses the `find()` method with a callback function to find an element in the array. The callback function checks if the current item is equal to 100,000. ```javascript const item = arr.find(item => item == 1E5); ``` 2. **"Array.prototype.findIndex"`**: This test case uses the `findIndex()` method to find the index of an element in the array that matches a given value (in this case, 100,000). If no match is found, it returns -1. ```javascript let item for (const i of arr) { if (i == 1E5) { item = i; return; } } ``` **Benchmark Results** The latest benchmark result shows the performance metrics for each test case: * **"Array.prototype.findIndex"`**: Executions per second (FPS): 16929.08 * **"for of"`**: FPS: 1392.43 These results indicate that `Array.prototype.findIndex()` is significantly faster than both traditional `for` loop with `indexOf()` and `for...of` loop. **Library** In this case, the `findIndex()` method is part of the JavaScript Standard Library, which means it's a built-in function available in all modern browsers without any additional libraries or dependencies. **Special JS Features or Syntax** There are no special features or syntaxes used in these test cases that require specific knowledge of JavaScript. However, keep in mind that some older browsers may not support `findIndex()` or `for...of` loops. **Other Alternatives** Some alternative approaches to finding an element in an array could be: * Using the `some()` method instead of `findIndex()` * Using a while loop with manual index checking * Using a recursive function approach However, it's worth noting that these alternatives are likely to be slower and less efficient than the methods used in this benchmark. In summary, this benchmark tests three different approaches to finding an element in an array: `Array.prototype.findIndex()`, traditional `for` loop with `indexOf()`, and `for...of` loop. The results show that `findIndex()` is significantly faster than the other two approaches.
Related benchmarks:
find vs findIndex (Array prototype methods)
find vs findIndex (Array prototype methods) 22
find vs findindex with condition test
!!find vs findIndex != -1
find vs indexOf (Array prototype methods)
Comments
Confirm delete:
Do you really want to delete benchmark?