Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test for loop
(version: 0)
Comparing performance of:
for of vs for let
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const testArray = []; for (let i = 0; i < 1000; i++) { testArray.push(i); } function executitionTask(elem) { return elem + 10 - 100; }
Tests:
for of
const testArray = []; for (let i = 0; i < 1000; i++) { testArray.push(i); } function executitionTask(elem) { return elem + 10 - 100; } for (const i of testArray) { executitionTask(i); }
for let
const testArray = []; for (let i = 0; i < 1000; i++) { testArray.push(i); } function executitionTask(elem) { return elem + 10 - 100; } for (let y = 0, len = testArray.length; i < len; i++) { executitionTask(i); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for of
for let
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. **Benchmark Definition JSON** The provided JSON represents a JavaScript microbenchmark test case named "test for loop". The benchmark measures the performance difference between two approaches: using `for...of` and `for...let`. **Options Compared** Two options are compared in this benchmark: 1. **`for...of`**: This syntax is used to iterate over arrays or iterable objects, such as strings or sets. 2. **`for...let`**: This syntax is used to declare a variable that will be used in the loop. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * `for...of`: + Pros: More concise and readable, as it eliminates the need for an explicit index variable. + Cons: Can be slower due to the overhead of creating an iterator object. * `for...let`: + Pros: Allows for easier iteration over arrays, as the loop index is directly accessible. + Cons: Less concise and less readable than `for...of`, requiring an additional variable declaration. **Library Used** There is no explicit library mentioned in the benchmark definition. However, it's likely that the `executitionTask` function uses a built-in JavaScript method or library to perform its operations. **Special JS Feature/Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. It only utilizes standard JavaScript syntax and built-in functions. **Other Alternatives** If you're looking for alternatives to these two approaches, consider the following: * `for...in`: This syntax is used to iterate over an object's own enumerable properties. However, it may not be suitable for arrays or other iterable objects. * `while` loops: These can be a good alternative when working with specific conditions or constraints that aren't easily expressed using a loop. **Benchmark Preparation Code** The provided script preparation code creates an array `testArray` and populates it with 1000 elements. It then defines the `executitionTask` function, which takes an element as input and returns its value after applying some transformation (in this case, simply adding 10 and subtracting 100). **Individual Test Cases** The two test cases are: 1. **`for of`**: This test case uses the `for...of` syntax to iterate over the `testArray`. 2. **`for let`**: This test case uses the `for...let` syntax to declare a variable `i` and loop through the `testArray`. Both test cases execute the same `executitionTask` function for each element in the array, measuring the execution time difference between the two approaches. **Benchmark Results** The latest benchmark results show that: * **`for let`**: The `for...let` approach outperforms the `for...of` approach, with an average of 232,046 executions per second. * **`for of`**: The `for...of` approach is slower than the `for...let` approach, with an average of 201,991.78 executions per second. These results suggest that, in this specific test case, using `for...let` provides a slight performance advantage over `for...of`. However, the actual performance impact may vary depending on the specific use case and requirements.
Related benchmarks:
testte
For i / For of / ForEach
js array perfomance
for vs foreach ej
Comments
Confirm delete:
Do you really want to delete benchmark?