【发布时间】:2011-06-01 08:59:14
【问题描述】:
我偶然发现了jQuery.fly() - flyweight pattern 性能基准,在查看了测试代码和the plugin code itself (also see below) 之后,我不知道它有什么用?我搜索了互联网,找不到任何关于插件本身的有用信息。
与在.each 中使用$(this) 相比,它是一种更有效的循环/迭代数组的方法吗?
-
使用 jQuery 对象进行迭代
a.each(function() { $(this); }); -
使用 jQuery.fly() 进行迭代
a.each(function() { $.fly(this); }); - 在 Firefox 4.0.1 中几乎 快 2 倍
- 在 Chrome 12 中几乎 快 3 倍
.飞
(function($) {
var fly = $(),
push = Array.prototype.push;
$.fly = function(elem) {
var len = fly.length,
i;
if ($.isArray(elem)) {
fly.length = 0;
i = push.apply(fly, elem);
} else {
if (elem instanceof $) {
return elem;
}
if (typeof elem == "string") {
throw "use jQuery()";
}
fly[0] = elem;
fly.length = i = 1;
}
// remove orphaned references
while (i<len) {
delete fly[i++];
}
return fly;
};
})(jQuery);
【问题讨论】:
-
由 jQuery 开发者查看 cmets:bugs.jquery.com/ticket/9481
标签: javascript jquery performance jquery-plugins