1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-30 05:19:57 +00:00

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.
This commit is contained in:
Jermolene 2017-02-21 08:31:05 +00:00
parent 8307f7c3ca
commit 9fc2086b71
3 changed files with 16 additions and 5 deletions

View File

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

View File

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

View File

@ -108,4 +108,17 @@ exports.isEqual = function(tiddler,excludeFields) {
return differences.length === 0; 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;
};
})(); })();