Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For of vs for each
(version: 0)
Comparing performance of:
For of vs Foreach
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
var arr = []; var size = 100000000; for (let i = 0; i < size; i += 1) { arr.push({a: i}); }
Tests:
For of
let twice = 0; for (const el of arr) { twice += 1; }
Foreach
let twice = 0; _.each(arr, function (el, index) { twice += 1; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
For of
Foreach
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.1:latest
, generated one year ago):
Let's dive into the provided JSON data and explain what's being tested. **Benchmark Definition** The benchmark is called "For of vs for each" which suggests that it's comparing two different ways to iterate over an array: `for...of` loop and the `_.each()` function from Lodash library (more on this later). **Test Cases** There are two test cases: 1. **For of**: This test case uses a `for...of` loop to iterate over an array `arr`. The loop is defined as: ```javascript for (const el of arr) { twice += 1; } ``` The key feature here is the use of `for...of` loop, which allows you to iterate over arrays and other iterable values. 2. **Foreach**: This test case uses the `_.each()` function from Lodash library to iterate over the same array `arr`. The code looks like this: ```javascript _.each(arr, function (el, index) { twice += 1; }); ``` The `_.each()` function is a part of the Lodash library, which provides various utility functions for working with arrays and objects. **Lodash Library** The Lodash library is a popular JavaScript library that provides various utility functions for tasks like data manipulation, string manipulation, array manipulation, etc. The `_.each()` function is one such utility function that allows you to iterate over an array or object and perform some operation on each element. **What's Being Compared?** In this benchmark, we're comparing the performance of two different ways to iterate over an array: * Using a `for...of` loop ( Test Case 1: For of) * Using the `_.each()` function from Lodash library (Test Case 2: Foreach) **Pros and Cons of Each Approach** Here are some pros and cons of each approach: * **For...of Loop** + Pros: - Easy to read and understand - Performs well for large arrays + Cons: - Not as flexible as other iteration methods (e.g., `for...in` loop) - May not work with non-array iterables (like sets or maps) * **_.each() Function** + Pros: - More flexible than `for...of` loop - Can handle non-array iterables (like sets or maps) - Provides additional features like `thisArg` and `context` options + Cons: - May have performance overhead due to the function call overhead **Other Considerations** When choosing between these two approaches, consider the following factors: * **Code Readability**: If you're writing code that needs to be easy to understand for other developers, a `for...of` loop might be a better choice. * **Flexibility**: If your iteration logic is complex or requires handling non-array iterables, the `_.each()` function might be more suitable. * **Performance**: In this benchmark, it seems like the `for...of` loop performs better than the `_.each()` function. However, this may not always be the case depending on specific use cases and environments. **Other Alternatives** Besides these two approaches, there are other ways to iterate over arrays in JavaScript: * **For...in Loop**: This is another iteration method that can work with array indices. * **Map() Function**: This function returns a new array created from iterating over an existing array or iterable. * **Filter() Function**: This function creates a new array containing all elements of the original array for which the provided testing function returns `true`. * **Reduce() Function**: This function applies a function to each element in an array, reducing it to a single value. Each of these alternatives has its own use cases and performance characteristics.
Related benchmarks:
Native vs Lodash.js contains
Lodash.js vs Native isArrary
Lodash.js vs Native _.min
Test native unique
Comments
Confirm delete:
Do you really want to delete benchmark?