Created
April 23, 2015 13:27
-
-
Save michelsalib/ebf56caccf128adcc1a8 to your computer and use it in GitHub Desktop.
Typescript @trace annotation
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
class Dog { | |
@trace | |
compute(a: number, b: number): any { | |
// my dog is a bit slow and weird to compute.. | |
for (var i = 0; i < 1000000000; i++) { | |
var result = a + b; | |
} | |
// .. not even accurate | |
return 'wouf'; | |
} | |
} | |
function trace(instance, name, descriptor) { | |
var original = descriptor.value; | |
descriptor.value = function() { | |
var traceName = instance.constructor.name + '.' + name; | |
console.time(traceName); | |
var result = original.apply(instance, arguments); | |
console.timeEnd(traceName); | |
return result; | |
} | |
return descriptor; | |
} | |
// create my dog | |
var d = new Dog(); | |
console.log(d.compute(10, 12)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment