Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Compute factorial of a number in JavaScript
(version: 7)
Iterative and recursive versions Implementations borrowed from https://medium.freecodecamp.org/how-to-factorialize-a-number-in-javascript-9263c89a4b38
Comparing performance of:
Finding factorial using loop vs Finding factorial using recursion
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function factorializeRecursive(num) { if (num < 0) return -1; else if (num == 0) return 1; else { return (num * factorializeRecursive(num - 1)); } } function factorializeLoop(num) { var result = num; if (num === 0 || num === 1) return 1; while (num > 1) { num--; result *= num; } return result; }
Tests:
Finding factorial using loop
var r = factorializeLoop(20);
Finding factorial using recursion
var r = factorializeRecursive(20);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Finding factorial using loop
Finding factorial using recursion
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/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Finding factorial using loop
13974992.0 Ops/sec
Finding factorial using recursion
854206.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to measure the performance of two different implementations of the factorial function in JavaScript: an iterative loop-based implementation (`factorializeLoop`) and a recursive implementation (`factorializeRecursive`). **Options Compared** Two options are compared: 1. **Iterative Loop-Based Implementation**: This approach uses a while loop to calculate the factorial by repeatedly multiplying the input number `num` with itself until it reaches 1. 2. **Recursive Implementation**: This approach uses function calls to recursively calculate the factorial by breaking down the problem into smaller sub-problems. **Pros and Cons of Each Approach** * **Iterative Loop-Based Implementation** + Pros: - Typically faster due to avoiding function call overhead - Can be more efficient in terms of memory usage + Cons: - May require more manual bookkeeping (e.g., managing the loop counter) - Can be more prone to errors if not implemented correctly * **Recursive Implementation** + Pros: - Easier to understand and implement for some developers - Can lead to elegant, concise code + Cons: - Typically slower due to function call overhead - May cause a stack overflow error if the input number is too large **Library/Functionality Used** In this benchmark, the `factorializeRecursive` function uses a recursive approach, while `factorializeLoop` uses an iterative loop-based approach. Both functions are defined in the provided script preparation code. **Special JS Feature/Syntax** The benchmark does not explicitly mention any special JavaScript features or syntax. However, it's worth noting that recursive functions can be affected by the call stack size limit, which may impact performance on older browsers or environments with limited resources. **Other Alternatives** Some alternative approaches to calculating factorials include: * **Using a library function**: Libraries like `Math.factorial()` (in some browsers) or third-party libraries (e.g., [mathjs](https://mathjs.org/)) can provide an efficient and convenient way to calculate factorials. * **Using parallel processing**: With the advent of multi-core processors, it's possible to use parallel processing techniques to speed up factorial calculations by dividing the work among multiple CPU cores. Keep in mind that these alternatives may have their own trade-offs in terms of performance, memory usage, and complexity.
Related benchmarks:
Factorials-comp
Factorials-comp
console.time overhead confirmed
Decimal rounding
Comments
Confirm delete:
Do you really want to delete benchmark?