Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Find in array - 004
(version: 0)
Comparing performance of:
For i++ (const size) vs For i++ (.length) vs For ... in vs for ... of vs indexOf vs findIndex vs Includes vs Some ? vs forEach vs set has
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var size = 10000 var arr = [] for (let i = 0; i < size; i++) { arr.push(i) } var research = 6578;
Tests:
For i++ (const size)
const a_size = arr.length; var index = -1; for (let y = 0; y < a_size; y++) { if (research === arr[y]) { index = y; break; } }
For i++ (.length)
var index = -1; for (let y = 0; y < arr.length; y++) { if (research === arr[y]) { index = y; break; } }
For ... in
var index = -1; for (const y in arr) { if (research === arr[y]) { index = y; break; } }
for ... of
var index = -1; for (const el of arr) { if (research === el) { index = el; break; } }
indexOf
var index = arr.indexOf(research)
findIndex
var index = arr.findIndex(e => e === research)
Includes
var found = arr.includes(research)
Some ?
var found = arr.some(e => e === research)
forEach
var index = -1; arr.forEach((el, y) => { if (research === el) { index = y; return; } })
set has
const found = new Set(arr).has(research)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (10)
Previous results
Fork
Test case name
Result
For i++ (const size)
For i++ (.length)
For ... in
for ... of
indexOf
findIndex
Includes
Some ?
forEach
set has
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 dive into the explanation of what is tested in the provided JSON and benchmark results. **Benchmark Definition** The benchmark definition represents an array search operation, where the goal is to find the index of a specific element (`research`) within the array `arr`. The script preparation code initializes an array of size 10,000 with numbers from 0 to 9,999, and sets the research value to 6,578. This creates a large dataset for testing various search algorithms. **Options Compared** The benchmark compares six different approaches to find the index of the `research` element in the array: 1. **For i++ (const size)**: A traditional for loop that iterates through each element in the array using an indexed approach. 2. **For ... in**: A for...in loop that iterates through each property in the object (in this case, the array). Note that arrays are not objects, but JavaScript will iterate over their indices as properties. 3. **For i++ (.length)**: Similar to the first option, but uses the length property of the array as the iterator variable. 4. **forEach**: The `forEach` method is called on each element in the array, allowing for execution of a provided callback function for each iteration. 5. **findIndex**: A built-in JavaScript method that returns the index of the first occurrence of the specified value (research). 6. **set has**: Although not directly applicable to arrays, I'll interpret it as using the `hasOwnProperty` method on the array's prototype, which would check if a property (i.e., an index) exists in the array. **Test Results** The benchmark results show the average number of executions per second for each option. The results suggest that: * **findIndex** is significantly faster than all other options. * **forEach` and `set has` are slower than traditional loops, but faster than `For i++ (.length)` and `For ... in`. * Traditional loops (`For i++ (const size)`) perform well due to their low overhead. Keep in mind that the actual performance can vary depending on various factors, such as hardware, JavaScript engine, and other concurrent processes.
Related benchmarks:
array last element big data
Test array and unshift
Speed of Arr.length and Slice
Getting last element of array
Array last index
Comments
Confirm delete:
Do you really want to delete benchmark?