Performance optimisations

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

Starts fixing #1864
print-window-tiddler
Jermolene 2015-07-05 19:47:44 +01:00
rodzic a86cfe2663
commit c6e48ebc2d
5 zmienionych plików z 54 dodań i 39 usunięć

Wyświetl plik

@ -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)
*/
$tw.utils.each = function(object,callback) {
var next,f;
var next,f,length;
if(object) {
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);
if(next === false) {
break;
}
}
} else {
for(f in object) {
if(Object.prototype.hasOwnProperty.call(object,f)) {
next = callback(object[f],f,object);
if(next === false) {
break;
}
var keys = Object.keys(object);
for (f=0, length=keys.length; f<length; f++) {
var key = keys[f];
next = callback(object[key],key,object);
if(next === false) {
break;
}
}
}

Wyświetl plik

@ -16,13 +16,16 @@ Filter operator returning all the tags of the selected tiddlers
Export our filter function
*/
exports.tags = function(source,operator,options) {
var results = [];
var tags = {};
source(function(tiddler,title) {
var t, length;
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);
};
})();

Wyświetl plik

@ -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:,}
*/
exports.parseWhiteSpace = function(source,pos) {
var node = {
type: "whitespace",
start: pos
};
var re = /(\s)+/g;
re.lastIndex = pos;
var match = re.exec(source);
if(match && match.index === pos) {
node.end = pos + match[0].length;
return node;
var p = pos,c;
while(true) {
c = source.charAt(p);
if((c === " ") || (c === "\f") || (c === "\n") || (c === "\r") || (c === "\t") || (c === "\v") || (c === "\u00a0")) { // Ignores some obscure unicode spaces
p++;
} else {
break;
}
}
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
*/
exports.skipWhiteSpace = function(source,pos) {
var whitespace = $tw.utils.parseWhiteSpace(source,pos);
if(whitespace) {
return whitespace.end;
var c;
while(true) {
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;
};
/*

Wyświetl plik

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

Wyświetl plik

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