forked from bahmutov/code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile-prototype-method.js
34 lines (26 loc) · 1.07 KB
/
profile-prototype-method.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
Almost the same as profile method call, but for wrapping methods that are on prototype
function Foo() {}
Foo.prototype.getName = function () { return this.name; }
var foo = new Foo();
foo.getName();
// profile getName without getting foo reference
profilePrototypeCall with proto = Foo.prototype;
and method name 'getName'
*/
(function profilePrototypeCall(proto, methodName) {
'use strict';
console.assert(proto, 'cannot find prototype to profile');
console.assert(typeof methodName === 'string', 'expected method name');
var originalMethod = proto[methodName];
console.assert(typeof originalMethod === 'function', 'cannot find method ' + methodName);
proto[methodName] = function () {
console.profile(methodName);
originalMethod.apply(this, arguments);
console.profileEnd(methodName);
proto[methodName] = originalMethod;
console.log('restored the prototype method call', methodName);
};
console.log('wrapped', methodName + ' in profiling calls');
// some prototype and method name
}(this.Photostack.prototype, '_rotateItem'));