Skip to content

Instantly share code, notes, and snippets.

@michelsalib
Created April 23, 2015 13:27
Show Gist options
  • Save michelsalib/ebf56caccf128adcc1a8 to your computer and use it in GitHub Desktop.
Save michelsalib/ebf56caccf128adcc1a8 to your computer and use it in GitHub Desktop.
Typescript @trace annotation
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