- if(!console){var console = {log : function(){},info : function(){},error:function(){},debug:function(){}}}
- var ListBuilder = function(parent, listLength) {
- this.parentEl = document.getElementById(parent);
- this.listLength = listLength;
- };
- ListBuilder.prototype = {
- buildList: function(tag,childTag) {
- var list = document.createElement(tag);
- this.parentEl.appendChild(list);
- for(var i = 0; i < this.listLength; i++) {
- var item = document.createElement(childTag);
- list.appendChild(item);
- }
- },
- removeLists:function(tag){
- var elements = document.getElementsByTagName(tag);
- for(var key =0;key<elements.length;key++){
- elements[key].parentNode.removeChild(elements[key]);
- }
- }
- };
- var MethodProfiler = function(component) {
- this.component = component;
- this.timers = {};
- for(var key in this.component) {
- // Ensure that the property is a function.
- if(typeof this.component[key] !== 'function') {
- continue;
- }
- // Add the method.
- var that = this;
- (function(methodName) {
- that[methodName] = function() {
- that.startTimer(methodName);
- var returnValue = that.component[methodName].apply(that.component,
- arguments);
- that.displayTime(methodName, that.getElapsedTime(methodName));
- return returnValue;
- };
- })(key); }
- };
- MethodProfiler.prototype = {
- startTimer: function(methodName) {
- this.timers[methodName] = (new Date()).getTime();
- },
- getElapsedTime: function(methodName) {
- return (new Date()).getTime() - this.timers[methodName];
- },
- displayTime: function(methodName, time) {
- console.log(methodName + ': ' + time + ' ms');
- }
- };
- /* Usage. */
- var list = new ListBuilder('list-container', 5000);
- list = new MethodProfiler(list);
- list.buildList('ol','li'); // Displays "buildList: 301 ms".
- list.buildList('ul','div'); // Displays "buildList: 287 ms".
- // list.removeLists('ul'); // Displays "removeLists: 10 ms".
- // list.removeLists('ol'); // Displays "removeLists: 12 ms".
- //该片段来自于http://www.codesnippet.cn/detail/2303201511968.html
来源: http://www.codesnippet.cn/detail/2303201511968.html