Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
else vs continue vs short if
(version: 0)
Comparing performance of:
else vs continue vs short if
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
else
let array = []; for (let i = 2000000; i > 0; i--) { if (i % 2) { array.push(1); } else { array.push(2); } }
continue
let array = []; for (let i = 2000000; i > 0; i--) { if (i % 2) { array.push(1); continue; } array.push(2) }
short if
let array = []; for (let i = 2000000; i > 0; i--) { array.push(i % 2 ? 1 : 2); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
else
continue
short if
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:141.0) Gecko/20100101 Firefox/141.0
Browser/OS:
Firefox 141 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
else
147.4 Ops/sec
continue
138.5 Ops/sec
short if
135.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Definition** The benchmark is called "else vs continue vs short if", which implies that it compares three different approaches to iterate over an array and push elements onto it based on certain conditions. **Test Cases** There are three individual test cases: 1. **"else"`: This test case uses a traditional `if-else` statement to check if the current number is even or odd, and pushes either 1 or 2 onto the array accordingly. 2. **"continue"`: In this test case, after pushing an element onto the array, the loop continues with the next iteration using the `continue` keyword. If the current number is even, it will be pushed onto the array, but then the loop will continue to the next iteration and check the next number. 3. **"short if"`: This test case uses a conditional operator (`i % 2 ? 1 : 2`) to determine which value to push onto the array in a single statement. **Library and Special Features** None of the test cases use any libraries or special JavaScript features beyond basic syntax and control structures (e.g., `for` loop, `if-else`, `continue`). **Options Compared** The three options being compared are: * **Traditional `if-else`**: Using a separate `if` statement to check if the current number is even or odd. * **Using `continue`**: After pushing an element onto the array, using the `continue` keyword to skip the rest of the loop body and move on to the next iteration. * **Short conditional operator**: Using a single conditional expression (`i % 2 ? 1 : 2`) to determine which value to push onto the array. **Pros and Cons** Here are some pros and cons for each approach: * **Traditional `if-else`**: + Pros: Easy to read and understand, clear intention. + Cons: May be slower due to the additional branch prediction overhead. * **Using `continue`**: + Pros: Can lead to faster loop execution, as the CPU can predict which branch will be taken more accurately. + Cons: May make the code harder to read and understand for some people. * **Short conditional operator**: + Pros: Often faster than traditional `if-else`, as it avoids branch prediction overhead. + Cons: May require some mental gymnastics to read and understand, especially for those unfamiliar with this syntax. **Other Considerations** When writing performance-critical code, other factors beyond just the loop iteration approach can impact performance. These include: * Array allocation and deallocation overhead * Loop iterations that are not predictable or cache-friendly * Cache locality and spatial memory organization These considerations highlight the importance of understanding how loops work in JavaScript (and most programming languages) to optimize performance. **Alternatives** Other alternatives for iterating over an array include: * **While loop**: Using a `while` loop with a counter variable that increments until the condition is met. * **For...of loop**: Using the new `for...of` loop syntax, which iterates over arrays using a more straightforward and expressive syntax. However, in this specific benchmark, only three approaches are being compared, so we'll focus on understanding those differences.
Related benchmarks:
JS if/if vs if/else if
If vs Switch
JS if/else vs if/else if
JS if/ vs if/else
BranchVsReturnTrue
Comments
Confirm delete:
Do you really want to delete benchmark?