
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Comparing the performance of recursive and looped factorial function in JavaScript
We will be writing two JavaScript functions, the job of both the functions will be to take in a number and return its factorial.
The first function should make use of a for loop or while loop to compute the factorial. Whereas the second function should compute the factorial using a recursive approach.
Lastly, we should compare the times taken by these functions over a large number of iterations.
Example
Following is the code −
const factorial = (num = 1) => { let result = 1; for (let i = 2; i <= num; i += 1) { result *= i; } return result; } const factorialRecursive = (num = 1) => { if(num > 1){ return num * factorialRecursive(num - 1); }else{ return 1; } }; const ITERATIONS = 100000000; const num = 12; console.time('Looping Approach'); for(let i = 0; i < ITERATIONS; i++){ factorial(num); }; console.timeEnd('Looping Approach'); console.time('Recursive Approach'); for(let j = 0; j < ITERATIONS; j++){ factorialRecursive(num); }; console.timeEnd('Recursive Approach');
Output
Following is the output on console −
Looping Approach: 886.720ms Recursive Approach: 6526.203ms
This time taken will vary from machine to machine by the ratio is bound to remain more or less the same.
- Related Articles
- How to write recursive Python Function to find factorial?
- Recursive factorial method in Java
- Function to compute factorial of a number in JavaScript
- How to write the factorial function with reduce and range in JavaScript?
- Sum of even numbers up to using recursive function in JavaScript
- Comparing and filling arrays in JavaScript
- Comparing adjacent element and swap - JavaScript?
- How to make a function that returns the factorial of each integer in an array JavaScript
- Factorial recursion in JavaScript
- Preimage Size of Factorial Zeroes Function in C++
- Comparing ascii scores of strings - JavaScript
- Calculating the sum of digits of factorial JavaScript
- How to improve the performance of JavaScript?
- Encoding decimal to factorial and back in JavaScript
- Comparing corresponding values of two arrays in JavaScript

Advertisements