Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For Loops1
(version: 0)
Testing the speeds of different for loops.
Comparing performance of:
Classic For Loop vs For Each
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script> let widest = {"x":222,"y":126,"width":1,"height":1,"color":"#000000","type":"rectangle"}; let map = [{"x":222,"y":126,"width":342,"height":103,"color":"#000000","type":"rectangle"},{"x":564,"y":229,"width":482,"height":-59,"color":"#000000","type":"rectangle"},{"x":870,"y":64,"width":176,"height":106,"color":"#000000","type":"rectangle"},{"x":1463,"y":99,"width":-1053,"height":421,"color":"#000000","type":"rectangle"},{"x":187,"y":373,"width":223,"height":147,"color":"#000000","type":"rectangle"},{"x":504,"y":709,"width":-199,"height":-96,"color":"#006400","type":"rectangle"},{"x":306,"y":614,"width":12,"height":-425,"color":"#000000","type":"rectangle"},{"x":765,"y":322,"width":-447,"height":-133,"color":"#FFFF00","type":"rectangle"},{"x":805,"y":832,"width":-375,"height":98,"color":"#FF0000","type":"rectangle"},{"x":431,"y":930,"width":-252,"height":-154,"color":"#000000","type":"rectangle"},{"x":65,"y":524,"width":114,"height":252,"color":"#FF4500","type":"rectangle"},{"x":900,"y":339,"width":60,"height":283,"color":"#FFFFFF","type":"rectangle"},{"x":960,"y":622,"width":-348,"height":-174,"color":"#000000","type":"rectangle"},{"x":804,"y":776,"width":-192,"height":-329,"color":"#800080","type":"rectangle"},{"x":873,"y":137,"width":17,"height":201,"color":"#006400","type":"rectangle"},{"x":890,"y":337,"width":-779,"height":-102,"color":"#000000","type":"rectangle"},{"x":148,"y":316,"width":-38,"height":-82,"color":"#FFFF00","type":"rectangle"},{"x":78,"y":141,"width":1,"height":0,"color":"#000000","type":"rectangle"},{"x":78,"y":141,"width":302,"height":363,"color":"#FF0000","type":"rectangle"},{"x":380,"y":504,"width":1,"height":0,"color":"#000000","type":"rectangle"},{"x":608,"y":723,"width":-173,"height":-250,"color":"#00FF00","type":"rectangle"},{"x":435,"y":473,"width":36,"height":-53,"color":"#000000","type":"rectangle"},{"x":270,"y":249,"width":201,"height":171,"color":"#FF4500","type":"rectangle"},{"x":684,"y":290,"width":107,"height":280,"color":"#0000FF","type":"rectangle"},{"x1":791,"y1":570,"x2":363,"y2":631,"color":"#000000","thickness":"1","type":"line"},{"x1":473,"y1":653,"x2":693,"y2":747,"color":"#000000","thickness":"1","type":"line"},{"x1":343,"y1":253,"x2":164,"y2":639,"color":"#000000","thickness":"1","type":"line"},{"x1":250,"y1":756,"x2":630,"y2":800,"color":"#000000","thickness":"1","type":"line"},{"x1":890,"y1":763,"x2":796,"y2":70,"color":"#000000","thickness":"1","type":"line"},{"x1":544,"y1":49,"x2":720,"y2":869,"color":"#000000","thickness":"1","type":"line"},{"x":80,"y":848,"radius":325.39975414864716,"color":"#000000","type":"circle"},{"x":796,"y":409,"radius":130.728726758888,"color":"#000000","type":"circle"},{"x":773,"y":771,"radius":88.54942122905152,"color":"#3eeb34","type":"circle"},{"x":416,"y":126,"radius":135.1184665395519,"color":"#3eeb34","type":"circle"},{"x":269,"y":854,"radius":118.9663818059539,"color":"#2338fc","type":"circle"},{"x":116,"y":292,"radius":132.9661611087573,"color":"#2338fc","type":"circle"},{"x":843,"y":344,"radius":111.8659912573969,"color":"#f22d95","type":"circle"},{"x":751,"y":111,"radius":41,"color":"#f22d95","type":"circle"},{"x":494,"y":465,"radius":136.23509092741122,"color":"#f22d95","type":"circle"}]; </script>
Tests:
Classic For Loop
var oSum = 0; for(var i in map) { oSum += map[i]; }
For Each
var oSum = 0; var oLength = map.length; for(var i = 0; i < oLength; i++) { oSum += map[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Classic For Loop
For 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):
Measuring the performance of different programming approaches is essential in software development. Let's break down what is tested on the provided JSON data and compare various options. **Benchmark Description** The benchmark measures the speed of two different for loop approaches: "Classic For Loop" and "For Each". The script prepares an array of rectangle objects with varying properties, such as x, y, width, height, color, and type. Two types of loops are implemented: 1. **Classic For Loop**: Iterates over the array using a traditional `for` loop, accessing each element directly. 2. **For Each**: Iterates over the array using the `forEach` method. **Options Compared** Two main options are compared in this benchmark: * **Traditional For Loop (Classic For Loop)**: This approach uses a traditional `for` loop with an index variable to iterate over the array elements. * **Iteration Using `forEach` Method (For Each)**: This approach leverages the built-in `forEach` method, which iterates over the array elements without requiring an explicit index variable. **Pros and Cons of Different Approaches** ### Traditional For Loop (Classic For Loop) Pros: * **Familiarity**: Developers are accustomed to using traditional `for` loops. * **Control**: This approach provides more control over the iteration process, such as accessing array elements directly. Cons: * **Performance**: Accessing array elements directly can be slower due to the need for indirection. * **Error-Prone**: Direct access increases the risk of off-by-one errors or incorrect indexing. ### Iteration Using `forEach` Method (For Each) Pros: * **Concise Code**: The `forEach` method provides a concise and readable way to iterate over array elements. * **Performance**: This approach can be faster, as it avoids direct access to array elements and uses optimized internal logic. Cons: * **Less Control**: The `forEach` method has less control over the iteration process compared to traditional `for` loops. * **Browser Support**: Not all browsers support the `forEach` method in older versions; this can affect compatibility. **Latest Benchmark Result** The latest benchmark result shows that the "For Each" approach outperforms the "Classic For Loop" approach, indicating a potential advantage of using the `forEach` method for iteration. In conclusion, when evaluating different programming approaches, it is essential to consider factors such as performance, readability, and compatibility. The choice between traditional `for` loops and the `forEach` method depends on the specific requirements of your project.
Related benchmarks:
map vs for: too much data
for vs foreach vs map 2
testspeed
Object.values Array.prototype.map vs Lodash.map
Object.entries Array.prototype.map vs Lodash.map
Comments
Confirm delete:
Do you really want to delete benchmark?