Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for loop comparison 2
(version: 0)
Comparing performance of:
native vs ES5 forEach (cannot break;) vs ES5 for-in vs ES6 for-of vs native (length in var) vs native (length in let)
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
Script Preparation code:
var values = [{a: 30310}, {b: 100303}, {c: 3040494}]
Tests:
native
var count = 0; for (var i = 0; i < values.length; i++) { if (values[i].a != null) { count++; } }
ES5 forEach (cannot break;)
var count = 0; values.forEach(function(v,i) { if (v.a != null) { count++; } });
ES5 for-in
var count = 0; for (value in values) { if (value.a != null) { count++; } }
ES6 for-of
var count = 0; for (value of values) { if (value.a != null) { count++; } }
native (length in var)
var count = 0; for (var i = 0, len=values.length; i < len; i++) { if (values[i].a != null) { count++; } }
native (length in let)
var count = 0; for (let i = 0, len=values.length; i < len; i++) { if (values[i].a != null) { count++; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
native
ES5 forEach (cannot break;)
ES5 for-in
ES6 for-of
native (length in var)
native (length in 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 break down the provided JSON and explain what is tested on MeasureThat.net. **Benchmark Definition** The benchmark is designed to compare the performance of different JavaScript loops in iterating over an array. The script preparation code sets up an array `values` with three elements, each containing a property `a`. The HTML preparation code includes a reference to the Lodash library, which is used later in the test cases. **Test Cases** There are six test cases, each representing a different JavaScript loop: 1. **native**: This test case uses a traditional `for` loop with a manual increment (`i = 0; i < values.length; i++`) and checks if the `a` property is not null. 2. **ES5 forEach (cannot break)**: This test case uses the `forEach` method, which is called without breaking out of the loop. It iterates over the array elements using a callback function and checks if the `a` property is not null. 3. **ES5 for-in**: This test case uses the `for...in` loop to iterate over the array elements, but it only accesses the properties, not the values. The loop checks if the `a` property is not null. 4. **ES6 for-of**: This test case uses the new `for...of` loop syntax to iterate over the array elements, which provides a more concise way of accessing the values. The loop checks if the `a` property is not null. 5. **native (length in var)**: This test case uses a traditional `for` loop with a manual increment (`i = 0; i < values.length; i++`) and checks if the `a` property is not null. However, it also includes the `let` keyword to declare the variable `len`, which allows for a more modern syntax. 6. **native (length in let)**: This test case is similar to the previous one, but it uses the `let` keyword instead of `var` to declare the variable `len`. **Libraries and Features** * Lodash library: Used in the HTML preparation code to provide the `forEach` method implementation. * `for...in` loop: A legacy JavaScript syntax that iterates over properties of an object, but it's not designed for array iteration. **Pros and Cons** Here are some pros and cons for each test case: 1. **native**: This is a traditional `for` loop approach, which can be more efficient than using `forEach` or `for...of`. However, it requires manual incrementing of the variable. 2. **ES5 forEach (cannot break)**: Using `forEach` without breaking out of the loop ensures that all elements are processed, but it may lead to performance issues if the loop has a large number of iterations. 3. **ES5 for-in**: This loop is designed for object iteration and may not be optimal for array iteration. 4. **ES6 for-of**: The new `for...of` loop syntax provides a concise way of accessing values, but it's only supported in modern browsers and engines. 5. **native (length in var)**: Using the `var` keyword with manual incrementing can lead to issues with variable scoping. 6. **native (length in let)**: This approach is more modern and safer than using `var`, as it avoids potential scope issues. **Performance** The performance results show that each test case has a different execution time, depending on the browser engine and version used. The traditional `for` loop (`native`) tends to perform well, while the newer loops (`ES5 forEach`, `ES6 for-of`) may have higher overhead due to the additional functionality provided by these methods. Keep in mind that performance results can vary greatly depending on the specific use case and implementation details.
Related benchmarks:
difference2
Lodash replace
lodash forEach vs for i loop modified
Comparing lodash's times with Array.from
JS ForEach Tests
Comments
Confirm delete:
Do you really want to delete benchmark?