Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Find in array - 003
(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 leSet = new Set(arr) 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 = leSet.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 world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The benchmark definition provided is a JSON object that describes the test case. Here, we're testing various ways to find an element in an array. The script preparation code creates a large array with 10,000 elements and sets the value of one of them to 6578, which is the target value for our search. **Options Compared** The benchmark compares six different approaches to find the index of a specific element in an array: 1. **For i++ (const size)**: Uses a traditional `for` loop with incrementing indices. 2. **For i++ (.length)**: Similar to the previous one, but uses the length property of the array to iterate. 3. **For ... in**: Uses the `in` operator to iterate over the array's properties. 4. **forEach**: Iterates over the array using the `forEach` method with a callback function. 5. **findIndex**: Uses the `findIndex` method, which returns the index of the first element that satisfies the provided condition. 6. **Includes** and **Some ?**: Not directly related to finding an element by its index, but rather checking if an element exists in the array using `includes` and `some`. **Pros and Cons** Here's a brief summary of each approach: * **For i++ (const size)**: Pros - simple and efficient; Cons - can be slow for large arrays due to incrementing indices. * **For i++ (.length)**: Similar pros and cons as the previous one, with an additional consideration that using `length` might incur a small overhead. * **For ... in**: Pros - potentially faster than traditional loops since it doesn't require incrementing indices; Cons - uses property access, which can be slower for large arrays. * **forEach**: Pros - concise and easy to read; Cons - might be slower due to the overhead of iterating over the array's elements. * **findIndex**: Pros - efficient and concise; Cons - assumes the element exists in the array, so it will throw an error if not found. * **Includes** and **Some ?**: These are not directly related to finding an element by its index. They're used for different purposes: `includes` checks if an element exists, while `some` checks if any elements satisfy a condition. **Benchmark Results** The benchmark results show the execution speed of each approach on Firefox 103. The fastest approaches are usually **findIndex**, which is designed to find an element in the array efficiently. However, this comes at the cost of assuming the element exists. In summary, when searching for an element by its index in a large array, `findIndex` is likely to be the best choice due to its efficiency and conciseness.
Related benchmarks:
Spliced array search
set.has vs. array.includes (1 million entries)
Set array expansion
Set array expansion Part 2
Set array expansion Part 3
Comments
Confirm delete:
Do you really want to delete benchmark?