1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-23 10:07:19 +00:00

Performance optimisations

In some situations, I’m seeing x2.5 speedups with these optimisations

Starts fixing #1864
This commit is contained in:
Jermolene 2015-07-05 19:47:44 +01:00
parent a86cfe2663
commit c6e48ebc2d
5 changed files with 54 additions and 39 deletions

View File

@ -62,22 +62,22 @@ $tw.utils.isDate = function(value) {
Iterate through all the own properties of an object or array. Callback is invoked with (element,title,object) Iterate through all the own properties of an object or array. Callback is invoked with (element,title,object)
*/ */
$tw.utils.each = function(object,callback) { $tw.utils.each = function(object,callback) {
var next,f; var next,f,length;
if(object) { if(object) {
if(Object.prototype.toString.call(object) == "[object Array]") { if(Object.prototype.toString.call(object) == "[object Array]") {
for (f=0; f<object.length; f++) { for (f=0, length=object.length; f<length; f++) {
next = callback(object[f],f,object); next = callback(object[f],f,object);
if(next === false) { if(next === false) {
break; break;
} }
} }
} else { } else {
for(f in object) { var keys = Object.keys(object);
if(Object.prototype.hasOwnProperty.call(object,f)) { for (f=0, length=keys.length; f<length; f++) {
next = callback(object[f],f,object); var key = keys[f];
if(next === false) { next = callback(object[key],key,object);
break; if(next === false) {
} break;
} }
} }
} }

View File

@ -16,13 +16,16 @@ Filter operator returning all the tags of the selected tiddlers
Export our filter function Export our filter function
*/ */
exports.tags = function(source,operator,options) { exports.tags = function(source,operator,options) {
var results = []; var tags = {};
source(function(tiddler,title) { source(function(tiddler,title) {
var t, length;
if(tiddler && tiddler.fields.tags) { if(tiddler && tiddler.fields.tags) {
$tw.utils.pushTop(results,tiddler.fields.tags); for(t=0, length=tiddler.fields.tags.length; t<length; t++) {
tags[tiddler.fields.tags[t]] = true;
}
} }
}); });
return results; return Object.keys(tags);
}; };
})(); })();

View File

@ -32,29 +32,39 @@ The exception is `skipWhiteSpace`, which just returns the position after the whi
Look for a whitespace token. Returns null if not found, otherwise returns {type: "whitespace", start:, end:,} Look for a whitespace token. Returns null if not found, otherwise returns {type: "whitespace", start:, end:,}
*/ */
exports.parseWhiteSpace = function(source,pos) { exports.parseWhiteSpace = function(source,pos) {
var node = { var p = pos,c;
type: "whitespace", while(true) {
start: pos c = source.charAt(p);
}; if((c === " ") || (c === "\f") || (c === "\n") || (c === "\r") || (c === "\t") || (c === "\v") || (c === "\u00a0")) { // Ignores some obscure unicode spaces
var re = /(\s)+/g; p++;
re.lastIndex = pos; } else {
var match = re.exec(source); break;
if(match && match.index === pos) { }
node.end = pos + match[0].length; }
return node; if(p === pos) {
return null;
} else {
return {
type: "whitespace",
start: pos,
end: p
}
} }
return null;
}; };
/* /*
Convenience wrapper for parseWhiteSpace. Returns the position after the whitespace Convenience wrapper for parseWhiteSpace. Returns the position after the whitespace
*/ */
exports.skipWhiteSpace = function(source,pos) { exports.skipWhiteSpace = function(source,pos) {
var whitespace = $tw.utils.parseWhiteSpace(source,pos); var c;
if(whitespace) { while(true) {
return whitespace.end; c = source.charAt(pos);
if((c === " ") || (c === "\f") || (c === "\n") || (c === "\r") || (c === "\t") || (c === "\v") || (c === "\u00a0")) { // Ignores some obscure unicode spaces
pos++;
} else {
return pos;
}
} }
return pos;
}; };
/* /*

View File

@ -18,7 +18,7 @@ exports.after = ["load-modules"];
exports.synchronous = true; exports.synchronous = true;
// Set to `true` to enable performance instrumentation // Set to `true` to enable performance instrumentation
var PERFORMANCE_INSTRUMENTATION = false; var PERFORMANCE_INSTRUMENTATION = true;
var widget = require("$:/core/modules/widgets/widget.js"); var widget = require("$:/core/modules/widgets/widget.js");

View File

@ -621,18 +621,20 @@ exports.getTiddlerData = function(titleOrTiddler,defaultData) {
tiddler = this.getTiddler(tiddler); tiddler = this.getTiddler(tiddler);
} }
if(tiddler && tiddler.fields.text) { if(tiddler && tiddler.fields.text) {
switch(tiddler.fields.type) { return this.getCacheForTiddler(tiddler.fields.title,"data",function() {
case "application/json": switch(tiddler.fields.type) {
// JSON tiddler case "application/json":
try { // JSON tiddler
data = JSON.parse(tiddler.fields.text); try {
} catch(ex) { data = JSON.parse(tiddler.fields.text);
return defaultData; } catch(ex) {
} return defaultData;
return data; }
case "application/x-tiddler-dictionary": return data;
return $tw.utils.parseFields(tiddler.fields.text); case "application/x-tiddler-dictionary":
} return $tw.utils.parseFields(tiddler.fields.text);
}
});
} }
return defaultData; return defaultData;
}; };