mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
PERF: speed up breakUp
breakUp function is complicated now, add a capped memoizer to ensure it runs a lot less.
This commit is contained in:
@@ -6,6 +6,48 @@ Discourse.Formatter = (function(){
|
|||||||
relativeAgeMedium, relativeAgeMediumSpan, longDate, toTitleCase,
|
relativeAgeMedium, relativeAgeMediumSpan, longDate, toTitleCase,
|
||||||
shortDate, shortDateNoYear, tinyDateYear, breakUp;
|
shortDate, shortDateNoYear, tinyDateYear, breakUp;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* memoize.js
|
||||||
|
* by @philogb and @addyosmani
|
||||||
|
* with further optimizations by @mathias
|
||||||
|
* and @DmitryBaranovsk
|
||||||
|
* perf tests: http://bit.ly/q3zpG3
|
||||||
|
* Released under an MIT license.
|
||||||
|
*
|
||||||
|
* modified with cap by Sam
|
||||||
|
*/
|
||||||
|
var cappedMemoize = function ( fn, max ) {
|
||||||
|
fn.maxMemoize = max;
|
||||||
|
fn.memoizeLength = 0;
|
||||||
|
|
||||||
|
return function () {
|
||||||
|
var args = Array.prototype.slice.call(arguments),
|
||||||
|
hash = "",
|
||||||
|
i = args.length;
|
||||||
|
currentArg = null;
|
||||||
|
while (i--) {
|
||||||
|
currentArg = args[i];
|
||||||
|
hash += (currentArg === Object(currentArg)) ?
|
||||||
|
JSON.stringify(currentArg) : currentArg;
|
||||||
|
if(!fn.memoize) {
|
||||||
|
fn.memoize = {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (hash in fn.memoize) {
|
||||||
|
return fn.memoize[hash];
|
||||||
|
} else {
|
||||||
|
fn.memoizeLength++;
|
||||||
|
if(fn.memoizeLength > max) {
|
||||||
|
fn.memoizeLength = 0;
|
||||||
|
fn.memoize = {};
|
||||||
|
}
|
||||||
|
var result = fn.apply(this, args);
|
||||||
|
fn.memoize[hash] = result;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
breakUp = function(str, hint){
|
breakUp = function(str, hint){
|
||||||
var rval = [];
|
var rval = [];
|
||||||
var prev = str[0];
|
var prev = str[0];
|
||||||
@@ -49,6 +91,8 @@ Discourse.Formatter = (function(){
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
breakUp = cappedMemoize(breakUp, 100);
|
||||||
|
|
||||||
shortDate = function(date){
|
shortDate = function(date){
|
||||||
return moment(date).shortDate();
|
return moment(date).shortDate();
|
||||||
};
|
};
|
||||||
@@ -240,6 +284,7 @@ Discourse.Formatter = (function(){
|
|||||||
updateRelativeAge: updateRelativeAge,
|
updateRelativeAge: updateRelativeAge,
|
||||||
toTitleCase: toTitleCase,
|
toTitleCase: toTitleCase,
|
||||||
shortDate: shortDate,
|
shortDate: shortDate,
|
||||||
breakUp: breakUp
|
breakUp: breakUp,
|
||||||
|
cappedMemoize: cappedMemoize
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user