Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs for vs while vs kijs.each
(version: 0)
Test array forEach, for, while, kijs.each
Comparing performance of:
forEach vs for vs while vs kijs.each
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://unpkg.com/kijs@0.0.2-b12/dist/kijs.umd.min.js'></script>
Tests:
forEach
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; arr.forEach(el => console.log(el));
for
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; for (let i = 0, len = arr.length; i < len; i++) { console.log(arr[i]); }
while
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; let i = 0; while (i < arr.length) { console.log(arr[i]); i++; }
kijs.each
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; kijs.each(arr, i=>console.log(i))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
forEach
for
while
kijs.each
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):
The provided benchmark measures the performance of four different approaches for iterating over an array: `forEach`, `for`, `while`, and `kijs.each`. **1. forEach** `forEach` is a built-in JavaScript method that executes a callback function once for each element in an array. The syntax is `arr.forEach((el) => { console.log(el); });`. This approach iterates over the entire array at once, processing all elements before moving on to the next one. Pros: * Easy to read and write * Built-in method, so it's unlikely to be deprecated Cons: * May not perform as well for large arrays due to its single-threaded nature and potential overhead from garbage collection. **2. For** The `for` loop is a traditional way of iterating over an array in JavaScript. The syntax is `for (let i = 0, len = arr.length; i < len; i++) { console.log(arr[i]); }`. This approach also iterates over the entire array, but it's often considered more efficient than `forEach` because it avoids potential overhead from callback invocation. Pros: * Can be optimized for performance by avoiding unnecessary iterations Cons: * Less readable and less concise than `forEach` **3. While** The `while` loop is another traditional way of iterating over an array in JavaScript. The syntax is `let i = 0; while (i < arr.length) { console.log(arr[i]); i++; }`. This approach also iterates over the entire array, but it can be less efficient than `for` because it involves repeated checks for termination. Pros: * Can be used with custom termination conditions Cons: * Less readable and less concise than `forEach` **4. kijs.each** `kijs.each` is a function from the Kijs library that provides a more functional programming style of iteration over arrays. The syntax is `kijs.each(arr, (i) => console.log(i));`. This approach iterates over the entire array in a way similar to `forEach`, but it uses a higher-order function to abstract away some of the iteration details. Pros: * Provides a concise and readable way to iterate over arrays * Can be optimized for performance by avoiding unnecessary iterations Cons: * Requires an external library (Kijs) to use, which may not be desirable in all situations. * May have slightly slower execution times compared to native methods like `forEach` or `for`. In the provided benchmark results, we can see that the execution times vary among different browsers and devices. However, it's generally observed that `kijs.each` performs the fastest, followed by `forEach`, `for`, and then `while`. This might be due to various factors such as cache efficiency, branch prediction, or optimization strategies used by each implementation. Other alternatives for iterating over arrays include: * Using `Array.prototype.reduce()` or `Array.prototype.forEach.call()` * Implementing custom iteration logic using closures or recursive functions * Utilizing WebAssembly (WASM) or other low-level programming paradigms to write highly optimized code Keep in mind that the performance differences between these approaches can vary depending on specific use cases, array sizes, and target hardware.
Related benchmarks:
For each vs some
for loop vs. lodash range foreach vs. jquery each
lodash.each vs Array.forEach vs jQuery.each vs for - function call
lodash.each vs Array.forEach vs jQuery.each vs for - inline code
for vs foreach vs for..of
Comments
Confirm delete:
Do you really want to delete benchmark?