标签:
function repush(array, item) {
// 对存在的item,更新到最后
// 用ii加速for循环
for (var i = 0, ii = array.length; i < ii; i++)
if (array[i] === item) {
return array.push(array.splice(i, 1)[0]);
}
}
function cacher(f, scope, postprocessor) {
// f是生成缓存的函数
// scope是f的域
// postprocessor是处理旧缓存的函数
// --------------------------------------
// 1e3是lru队列的量,用短路方法(&&)执行
function newf() {
var arg = Array.prototype.slice.call(arguments, 0),
args = arg.join("\u2400"),
cache = newf.cache = newf.cache || {},
count = newf.count = newf.count || [];
if (cache.hasOwnProperty(args)) {
repush(count, args);
return postprocessor ? postprocessor(cache[args]) : cache[args];
}
count.length >= 1e3 && delete cache[count.shift()];
count.push(args);
cache[args] = f.apply(scope, arg);
return postprocessor ? postprocessor(cache[args]) : cache[args];
}
return newf;
}
(function(root, factory) {
if (typeof define === ‘function‘ && define.amd) {
define([], factory);
} else if (typeof exports === ‘object‘) {
module.exports = factory();
} else if (typeof define === "function" && define.cmd) {
define(function(require, exports, module) {
module.exports = factory();
});
} else {
root.Sword = factory();
}
}(this, function() {
}))
标签:
原文地址:http://my.oschina.net/u/1402334/blog/500810