Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs do while 2
(version: 1)
Comparing performance of:
//1.case For i vs 2 do while
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var newBody = document.createElement("div"); for (let i; i < 1000; i++) { var divAdded = document.createElement("div"); divAdded.classList.add("ggclass"); newBody.appendChild(divAdded); }
Tests:
//1.case For i
var case1 = document.getElementsByClassName("ggclass"); for (var i = 0; i < case1.length; i++) { // 반복할 내용 console.log(case1[i]); }
2 do while
var case1 = document.getElementsByClassName("ggclass"); do { console.log(case1[i]); i++ } while (i < case1.length);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
//1.case For i
2 do while
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
//1.case For i
4789710.5 Ops/sec
2 do while
157628.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark tests the performance difference between two loops: a traditional `for` loop and a `do-while` loop, both used to iterate over an array of HTML elements (`case1`) that contain the class "ggclass". **Loop Options Compared** The benchmark compares two approaches: 1. **Traditional For Loop**: The code uses a `for` loop with a variable `i`, initialized before the loop, and increments after each iteration. ```javascript for (let i; i < 1000; i++) { // ... } ``` Pros: * Easy to understand and write for developers familiar with traditional loops. * Can be optimized using techniques like loop unrolling or caching. Cons: * May have slower execution due to the overhead of initializing variables before each iteration. 2. **Do-While Loop**: The code uses a `do-while` loop that executes the body only once, and then checks the condition before the next iteration. ```javascript do { console.log(case1[i]); i++; } while (i < case1.length); ``` Pros: * Can be faster since it doesn't require initializing variables on each iteration. Cons: * May have slower performance due to the need for a conditional check after each execution, which can lead to branch prediction errors in some cases. **Library Usage** The benchmark uses the `document` object and its methods to access the HTML elements. Specifically, it uses `getElementsByClassName()` to retrieve an array of elements with the class "ggclass". Pros: * Provides a convenient way to access DOM elements in JavaScript. * Allows for easy modification of the DOM structure. Cons: * May have slower performance due to the overhead of accessing the DOM. * Can be sensitive to changes in the DOM structure or layout. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. The code is written in standard ECMAScript. **Alternative Approaches** If you want to explore alternative approaches, here are a few options: 1. **Using `Array.prototype.forEach()`**: Instead of using a loop, you can use the `forEach` method to iterate over an array. ```javascript case1.forEach(element => console.log(element)); ``` This approach is often faster and more readable than traditional loops. 2. **Using `Array.prototype.map()`**: If you want to perform some operation on each element of the array, you can use the `map` method. ```javascript const results = case1.map(element => console.log(element)); ``` This approach is often faster and more concise than using a loop or `forEach`. 3. **Using a microbenchmarking library**: If you want to create a more comprehensive benchmark, you can consider using a dedicated microbenchmarking library like Benchmark.js or Microbenchmark. Keep in mind that the choice of approach depends on your specific use case, performance requirements, and personal preference.
Related benchmarks:
querySelectorAll vs getElementsByClassName with 1,000,000 children searched
insertAdjacentHTML VS append VS appendChild (createDocumentFragment) [MINIMAL]
getElementsByClassName, For i, For of, For each, Some, Every, Map 2
for i inside or outside var 3
Comments
Confirm delete:
Do you really want to delete benchmark?