1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-12 19:20:27 +00:00
TiddlyWiki5/core/modules/utils/performance.js
Jermolene d402d3c5a6 Add performance measurement features
This is an experimental module to help us measure the performance of
the refresh cycle and the filter mechanism. Not intended to replace the
performance measurement features in browser developer tools, just to
make it easier to automate performance checks cross-browser.

The immediate purpose is to help in refactoring the filter mechanism.
The recent change to encapsulate the wiki store “tiddlers” object has
hurt the performance of filters, and it’s going to be helpful to have
decent measurements while refactoring that code.

I’m still not convinced that this stuff should be in the core, and may
well end up removing it after the present refactoring cycle.
2014-04-01 08:33:36 +01:00

63 lines
1.3 KiB
JavaScript

/*\
title: $:/core/modules/utils/performance.js
type: application/javascript
module-type: global
Performance measurement.
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
function Performance(enabled) {
this.enabled = !!enabled;
this.measures = {}; // Hashmap of current values of measurements
this.logger = new $tw.utils.Logger("performance");
}
/*
Wrap performance reporting around a top level function
*/
Performance.prototype.report = function(name,fn) {
var self = this;
if(this.enabled) {
return function() {
self.measures = {};
var startTime = $tw.utils.timer(),
result = fn.apply(this,arguments);
self.logger.log(name + ": " + $tw.utils.timer(startTime) + "ms");
for(var m in self.measures) {
self.logger.log("+" + m + ": " + self.measures[m] + "ms");
}
return result;
};
} else {
return fn;
}
};
/*
Wrap performance measurements around a subfunction
*/
Performance.prototype.measure = function(name,fn) {
var self = this;
if(this.enabled) {
return function() {
var startTime = $tw.utils.timer(),
result = fn.apply(this,arguments),
value = self.measures[name] || 0;
self.measures[name] = value + $tw.utils.timer(startTime);
return result;
};
} else {
return fn;
}
};
exports.Performance = Performance;
})();