Optimise sameday filter

I used this test:

console.time();for(var t=0; t<200; t++)
{$tw.wiki.filterTiddlers("[all[tiddlers+shadows]sameday[20170210]]");};c
onsole.timeEnd()

Before this patch, I got speeds of approx 190ms, versus 140ms
afterwards.

Note that the ability to add a cache property like this is only
possible because tiddler objects are immutable.
print-window-tiddler
Jermolene 2017-02-21 08:31:05 +00:00
rodzic 8307f7c3ca
commit 9fc2086b71
3 zmienionych plików z 16 dodań i 5 usunięć

Wyświetl plik

@ -827,6 +827,7 @@ taking precedence to the right
*/
$tw.Tiddler = function(/* [fields,] fields */) {
this.fields = Object.create(null);
this.cache = Object.create(null);
for(var c=0; c<arguments.length; c++) {
var arg = arguments[c],
src = (arg instanceof $tw.Tiddler) ? arg.fields : arg;

Wyświetl plik

@ -20,12 +20,9 @@ exports.sameday = function(source,operator,options) {
fieldName = operator.suffix || "modified",
targetDate = (new Date($tw.utils.parseDate(operator.operand))).setHours(0,0,0,0);
// Function to convert a date/time to a date integer
var isSameDay = function(dateField) {
return (new Date(dateField)).setHours(0,0,0,0) === targetDate;
};
source(function(tiddler,title) {
if(tiddler && tiddler.fields[fieldName]) {
if(isSameDay($tw.utils.parseDate(tiddler.fields[fieldName]))) {
if(tiddler) {
if(tiddler.getFieldDay(fieldName) === targetDate) {
results.push(title);
}
}

Wyświetl plik

@ -108,4 +108,17 @@ exports.isEqual = function(tiddler,excludeFields) {
return differences.length === 0;
};
exports.getFieldDay = function(field) {
if(this.cache && this.cache.day && $tw.utils.hop(this.cache.day,field) ) {
return this.cache.day[field];
}
var day = "";
if(this.fields[field]) {
day = (new Date($tw.utils.parseDate(this.fields[field]))).setHours(0,0,0,0);
}
this.cache.day = this.cache.day || {};
this.cache.day[field] = day;
return day;
};
})();