Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ee75aa6

Browse files
committed
added prototype method profiling snippet
1 parent c2c12fe commit ee75aa6

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Read [Code Snippets tutorial][1],
3737
from [addyosmani/timing.js](https://github.com/addyosmani/timing.js).
3838
* [time-method-call.js](time-method-call.js) - measures single method call time.
3939
* [profile-method-call.js](profile-method-call.js) - profiles a single method call.
40+
* [profile-prototype-method.js](profile-prototype-method.js) - profiles a single method call
41+
that is on a prototype object, not on an instance.
4042

4143
### Storage measurements
4244

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "code-snippets",
33
"main": "first-paint.js",
4-
"version": "0.3.0",
4+
"version": "0.3.1",
55
"homepage": "https://github.com/bahmutov/code-snippets",
66
"license": "MIT",
77
"ignore": [

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "code-snippets",
33
"description": "Chrome DevTools code snippets ",
4-
"version": "0.3.0",
4+
"version": "0.3.1",
55
"author": "Gleb Bahmutov <[email protected]>",
66
"bugs": {
77
"url": "https://github.com/bahmutov/code-snippets/issues"

profile-prototype-method.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Almost the same as profile method call, but for wrapping methods that are on prototype
3+
function Foo() {}
4+
Foo.prototype.getName = function () { return this.name; }
5+
var foo = new Foo();
6+
foo.getName();
7+
8+
// profile getName without getting foo reference
9+
profilePrototypeCall with proto = Foo.prototype;
10+
and method name 'getName'
11+
*/
12+
(function profilePrototypeCall(proto, methodName) {
13+
'use strict';
14+
15+
console.assert(proto, 'cannot find prototype to profile');
16+
console.assert(typeof methodName === 'string', 'expected method name');
17+
18+
var originalMethod = proto[methodName];
19+
console.assert(typeof originalMethod === 'function', 'cannot find method ' + methodName);
20+
21+
proto[methodName] = function () {
22+
console.profile(methodName);
23+
24+
originalMethod.apply(this, arguments);
25+
26+
console.profileEnd(methodName);
27+
28+
proto[methodName] = originalMethod;
29+
console.log('restored the prototype method call', methodName);
30+
};
31+
console.log('wrapped', methodName + ' in profiling calls');
32+
33+
// some prototype and method name
34+
}(this.Photostack.prototype, '_rotateItem'));

0 commit comments

Comments
 (0)