Last active
June 7, 2019 18:13
-
-
Save kshitijpurwar/22b08eae0b3312f4abd8416e02405c4e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is the decorator function that times our functions | |
function timer(target, name, descriptor) { | |
console.log(target, name, descriptor); | |
let original = descriptor.value; | |
descriptor.value = function(...args){ | |
console.time('timer'); | |
var result = original.apply(this, args); | |
console.timeEnd('timer'); | |
return result; | |
} | |
return descriptor; | |
} | |
class Test { | |
constructor() { | |
let a = 50; | |
console.log(this.recursiveFactorial(a)); | |
console.log(this.loopingFactorial(a)); | |
} | |
@timer | |
recursiveFactorial(x){ | |
function factorial(num){ | |
if(num===0 || num===1 ) { | |
return 1; | |
} | |
return num * factorial(num-1); | |
} | |
return factorial(x); | |
} | |
@timer | |
loopingFactorial(x){ | |
if(x == 0) { | |
return 1; | |
} | |
if(x < 0 ) { | |
return undefined; | |
} | |
for(var i = x; --i; ) { | |
x *= i; | |
} | |
return x; | |
} | |
} | |
let test = new Test(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment