Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Factorial math
(version: 3)
Comparing performance of:
Recursive vs While loop vs While loop (other)
Created:
one year ago
by:
Registered User
Jump to the latest result
Tests:
Recursive
function pangkatRec(x, y) { if (y < 0) { return 0 } else if (y == 0) { return 1 } else { return x * pangkatRec(x, y - 1) } } pangkatRec(2, 3)
While loop
function pangkatImperatives(x, y) { if (y < 0) { return 0; } else if (y === 0) { return 1; } else { let result = 1; while (y > 0) { result *= x; y--; } return result; } } pangkatImperatives(2, 3)
While loop (other)
function pangkatImperatives(x, y) { let n = y + 1; // n+1 let result = 0; while (n--) { if (result < 0) { result = 0 } else if (result == 0){ result = 1 } else { result *= x; } } return result } pangkatImperatives(2, 3)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Recursive
While loop
While loop (other)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
pHqghUme
1.0 Ops/sec
pHqghUme
1.0 Ops/sec
pHqghUme
1.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and its implications. **Benchmark Definition** The benchmark definition consists of a single test case, which is a mathematical function `pangkatRec` or one of its variants (`pangkatImperatives`). The function calculates the power of a number using recursion or loops. There are three versions: 1. Recursive version (`pangkatRec`) 2. Imperative version with a while loop (`pangkatImperatives (first variant)`) 3. Imperative version with another while loop structure (`pangkatImperatives (second variant)`) **What is being tested?** The benchmark tests the performance of each version of the `pangkat` function, measuring how many executions per second it can handle on a specific device (in this case, Chrome 121 on Windows). **Options compared** The benchmark compares three different approaches: 1. **Recursive approach**: This approach uses recursive function calls to calculate the power. 2. **Imperative approach with while loop 1**: This approach uses a traditional while loop to iterate from `y` down to 0, multiplying the result by `x` in each iteration. 3. **Imperative approach with while loop 2**: This approach has a slightly different structure for the while loop compared to the first variant. **Pros and Cons** Here are some pros and cons of each approach: 1. Recursive approach: * Pros: Easy to implement, no explicit loop required. * Cons: Can lead to stack overflow errors if `y` is too large, can be less efficient due to repeated function calls. 2. Imperative approach with while loop 1: * Pros: Typically more efficient than recursive approaches, as it avoids repeated function calls and uses a simple loop structure. * Cons: May require more manual memory management and loop optimizations. 3. Imperative approach with while loop 2: * Pros: Similar to the first variant, but with a slightly different loop structure (no clear advantage). * Cons: Same as the previous variant. **Library usage** There is no explicit library mentioned in the benchmark definition or test cases. **Special JS features or syntax** None of the provided benchmarks explicitly use special JavaScript features like async/await, generators, or decorators. The focus seems to be on comparing different algorithmic approaches for calculating powers. **Other alternatives** If you were to implement a similar benchmark for other mathematical functions, you might consider exploring: * Iterative approaches (e.g., using `for` loops or bitwise operations) * Dynamic programming techniques * Memoization or caching to reduce redundant calculations * Using specialized libraries or hardware acceleration (if applicable) Keep in mind that the choice of approach depends on the specific problem and performance requirements.
Related benchmarks:
factorial
Factorials-comp
Compute factorial of a number in JavaScript
console.time overhead confirmed
Comments
Confirm delete:
Do you really want to delete benchmark?