Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
finding array element
(version: 0)
Comparing performance of:
foreach vs for..in vs for..of vs for (no cached length) vs for (cached length) vs find
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
foreach
const a = [0, 1, 2, 4, 5, 6, 8, 9, 10, 11, 13, 14, 15]; const number = Math.random()*15; a.forEach((t) => { if (t == number) return t; }); return undefined;
for..in
const a = [0, 1, 2, 4, 5, 6, 8, 9, 10, 11, 13, 14, 15]; const number = Math.random()*15; for (const t in a){ if (t === number) return t;} return undefined;
for..of
const a = [0, 1, 2, 4, 5, 6, 8, 9, 10, 11, 13, 14, 15]; const number = Math.random()*15; for (const t of a){ if (t === number) return t;} return undefined;
for (no cached length)
const a = [0, 1, 2, 4, 5, 6, 8, 9, 10, 11, 13, 14, 15]; const number = Math.random()*15; for (let i = 0; i < a.length; i++){ if (a[i] === number) return a[i];} return undefined;
for (cached length)
const a = [0, 1, 2, 4, 5, 6, 8, 9, 10, 11, 13, 14, 15]; const number = Math.random()*15; const length = a.length; for (let i = 0; i < length; i++){ if (a[i] === number) return a[i];} return undefined;
find
const a = [0, 1, 2, 4, 5, 6, 8, 9, 10, 11, 13, 14, 15]; const number = Math.random()*15; return a.find(t => t === number);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
foreach
for..in
for..of
for (no cached length)
for (cached length)
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):
Measuring the performance of JavaScript loops is an essential task, and MeasuringThat.net provides a great platform for it. The provided benchmark tests four different ways to find an element in an array: 1. `foreach` loop: - The code uses the traditional `forEach` loop syntax, where each iteration checks if the current element matches the target value. - This approach is generally slower due to the overhead of checking each element individually. Pros: Easy to understand and implement, suitable for small arrays or simple use cases. Cons: Slower compared to other methods, may be inefficient for large arrays. 2. `for..in` loop: - The code uses the `for...in` loop syntax, which iterates over the array's indices instead of its elements. - This approach can lead to slower performance due to the overhead of accessing indices and comparing them with the target value. Pros: Can be faster than traditional loops for very large arrays (since it only needs to access indices), suitable for arrays where element access is expensive. Cons: Less intuitive for developers who are not familiar with `for...in`, may lead to off-by-one errors if not used correctly. 3. `for..of` loop: - The code uses the new `for...of` loop syntax, which iterates over the array's elements. - This approach is generally faster than traditional loops since it avoids the overhead of checking each element individually. Pros: Faster than traditional loops, suitable for arrays where element access is cheap. Cons: Less intuitive for developers who are not familiar with `for...of`, only supported in modern JavaScript versions. 4. Manual indexing (`for` loop): - The code uses a traditional `for` loop with manual indexing to iterate over the array. - This approach can be slower than other methods since it requires explicit indexing and comparison operations. Pros: More control over iteration, suitable for arrays where element access is expensive or has side effects. Cons: Less efficient compared to other methods, may lead to off-by-one errors if not used correctly. Other alternatives that are not tested in this benchmark include: - Using `Array.prototype.indexOf()` method, which can be slower than the manual indexing approach due to its additional overhead. - Using a library like Lodash or Ramda for array operations, which may introduce additional dependencies and performance overheads. The provided benchmark results indicate that the `for..of` loop is generally the fastest approach, followed by the manual indexing (`for` loop) method. However, the actual performance difference between these methods can vary depending on the specific use case and array size.
Related benchmarks:
findIndex vs for-of
Array.find(false) vs for..of
Find in array
JS find vs indexOf 3
array.find and boolean cast or array.some
Comments
Confirm delete:
Do you really want to delete benchmark?