JQuery 映射 vs Javascript 映射 vs For 循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6551139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery map vs Javascript map vs For-loop
提问by dave
I'm implementing some code that is a natural fit for map. However, I have a significant amount of objects in a list that I'm going to iterate through, so my question is which is the best way to go abou this:
我正在实现一些自然适合地图的代码。但是,我将要遍历的列表中有大量对象,所以我的问题是哪种方法最好:
var stuff = $.map(listOfMyObjects, someFunction())
var stuff = listOfMyObjects.map(someFunction())
or just
要不就
var stuff = new Array();
for(var i = 0; i < listOfmyObjects.length; i++){
stuff.push(someFunction(listOfMyObjects[i]));
}
采纳答案by tskuzzy
The latter (for loop) is much faster. I remember seeing a benchmark somewhere but I can't seem to find the link.
后者(for 循环)要快得多。我记得在某处看到过一个基准测试,但我似乎找不到链接。
If performance is really an issue then I would use the for loop. It doesn't really obscure the code that much.
如果性能确实是一个问题,那么我将使用 for 循环。它并没有真正掩盖代码。
回答by Socram
here is a test case done in jsben.ch: http://jsben.ch/#/BQhED
这是在 jsben.ch 中完成的测试用例:http://jsben.ch/#/BQhED
it shows that a for-loop map is faster than a jquery map (at least in chrome).
它表明 for 循环映射比 jquery 映射(至少在 chrome 中)更快。
回答by jAndy
First at all, true Objects
don't have a native .map()
method, neither a .length
property. So we are either talking about Arrays
or Array-like-objects
(jQuery objects for instance).
首先, trueObjects
没有本地.map()
方法,也没有.length
属性。所以我们要么谈论Arrays
或Array-like-objects
(例如jQuery 对象)。
However, there is not faster way to iterate than using a native for
, while
or do-while
loop. All other functional operations do performan (guess what) a function for each iteration which costs.
然而,有没有更快的方式比迭代使用本机for
,while
或do-while
循环。所有其他函数操作都会为每次迭代执行(猜猜是什么)一个函数,这会产生成本。
jQuerys 's .each()
will just performan a for-in
loop when an object is passed to it. That is fairly the fastest way to loop over an object. You could just use a for-in
yourself and you save the overhead call.
当一个对象传递给jQuery时,它.each()
只会执行一个for-in
循环。这是循环对象的最快方法。您可以for-in
自己使用,并节省开销调用。
Another "good" way in terms of readabilty is to make usage of ES5 features like .keys()
and .map()
. For instance:
另一个提高可读性的“好”方法是使用 ES5 特性,如.keys()
和.map()
。例如:
var myObj = {
foo: 'bar',
base: 'ball',
answer: 42,
illuminati: 23
};
Object.keys( myObj ).map(function( prop ) {
console.log( myObj[ prop ] );
});
Which I think is a very good tradeof in terms of readabilty, convinience and performance. Of course you should use an ES5 abstraction library for old'ish browser.
我认为这是在可读性、方便性和性能方面非常好的权衡。当然,您应该为旧浏览器使用 ES5 抽象库。
But again, no way to beat native loops in terms of performance.
但同样,在性能方面没有办法击败本机循环。
回答by InfinitiesLoop
+1 for the "test it" answer by Emil :) That's always the right answer.
+1 为 Emil 的“test it”答案:) 这始终是正确的答案。
But yeah, native loops win, and you can do one better by caching the length so the .length reference isn't evaluated each iteration.
但是,是的,本地循环获胜,您可以通过缓存长度来做得更好,这样 .length 引用就不会在每次迭代时进行评估。
for(var i = 0, l = list.length; i < l; i++)
or avoid the extra var by doing it backwards
或通过向后执行来避免额外的 var
for(var i = list.length-1; i >= 0; i--)
And, if you can 'inline' the 'someFunction', that will be faster still. Basically, avoid function calls and references as much as you can. But that's only if you are really looking at fine detail. It's unlikely optimizations like this are going to matter much. Always test to find your bottlenecks.
而且,如果您可以“内联”“someFunction”,那会更快。基本上,尽可能避免函数调用和引用。但这只有在您真正关注细节时才会如此。像这样的优化不太可能很重要。始终进行测试以找到瓶颈。
回答by Emil
Create a test cases with your html/javascript code at jsperf.
在jsperf使用您的 html/javascript 代码创建测试用例。
You will be able to see what works best, and how fast different browsers perform the loops.
您将能够看到什么效果最好,以及不同浏览器执行循环的速度。
I would put my money on the native JavaScript loop, but you never know.
我会把钱花在原生 JavaScript 循环上,但你永远不知道。