Skip to content

Instantly share code, notes, and snippets.

@kshitijpurwar
Last active June 7, 2019 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kshitijpurwar/22b08eae0b3312f4abd8416e02405c4e to your computer and use it in GitHub Desktop.
Save kshitijpurwar/22b08eae0b3312f4abd8416e02405c4e to your computer and use it in GitHub Desktop.
// 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