discourse/vendor/assets/javascripts/ember-renderspeed.js

101 lines
2.5 KiB
JavaScript
Raw Normal View History

//
// ember-renderspeed
//
// Include this script if you want to instrument your rendering speed in your Ember
// applications.
//
// https://github.com/eviltrout/ember-renderspeed
//
if ((typeof console !== 'undefined') && console.groupCollapsed && !window.QUnit) {
(function () {
/**
Used for assembling a tree of render calls so they can be grouped and displayed
nicely afterwards.
@class ProfileNode
**/
var ProfileNode = function(start, payload) {
this.start = start;
this.payload = payload;
this.children = [];
};
/**
2013-08-05 16:15:16 -04:00
Adds a child node underneath this node.
@method addChild
@param {ProfileNode} node the node we want as a child
**/
ProfileNode.prototype.addChild = function(node) {
this.children.push(node);
};
/**
Logs this node and any children to the developer console, grouping appropriately
for easy drilling down.
@method log
**/
ProfileNode.prototype.log = function(type) {
var description = "";
if (this.payload) {
if (this.payload.template) {
description += "'" + this.payload.template + "' ";
}
if (this.payload.object) {
description += this.payload.object.toString() + " ";
}
}
2013-08-05 16:15:16 -04:00
description += (Math.round(this.time * 100) / 100).toString() + "ms";
if (this.children.length === 0) {
console.log(type + ": " + description);
} else {
// render a collapsed group when there are children
console.groupCollapsed(type + ": " + description);
this.children.forEach(function (c) {
c.log(type);
});
console.groupEnd();
}
}
// Set up our instrumentation of Ember below
Ember.subscribe("render", {
depth: null,
before: function(name, timestamp, payload) {
var node = new ProfileNode(timestamp, payload);
2013-08-05 16:15:16 -04:00
if (this.depth) { node.parent = this.depth; }
this.depth = node;
return node;
},
after: function(name, timestamp, payload, profileNode) {
2013-08-05 16:15:16 -04:00
if (payload.exception) {
throw payload.exception;
}
2013-08-05 16:15:16 -04:00
var parent = profileNode.parent;
profileNode.time = (timestamp - profileNode.start);
this.depth = profileNode.parent;
2013-08-05 16:15:16 -04:00
2013-08-05 16:20:36 -04:00
if (profileNode.time < 1) { return; }
2013-08-05 16:20:36 -04:00
if (this.depth) {
this.depth.addChild(profileNode);
} else {
profileNode.log("Render");
}
}
});
})();
2013-08-05 16:15:16 -04:00
}