PDA

View Full Version : How does exactly work the Ext.each statement?



ssamuel68
13 Sep 2013, 12:12 PM
Hi,

I know they are several functions on javascript for looping.

Ext.each is one provided for Sencha. I want to know what is the plain JS equivalent.

On this page I found a benchmark for loop functions: http://jsperf.com/fors-vs-while/58

F (http://jsperf.com/fors-vs-while/58)or example:



values.forEach(add);

for (i = 0; i < values.length; i++) {
add(values[i]);
}

var i = 0,
n = values.length;
while (i < n) {
sum += values[i++];
}So, which one of this are the Ext.each equivalent?

Thx.

evant
13 Sep 2013, 5:05 PM
Not really sure what you're asking. All of them end up iterating over an array and doing something.

Ext.each would be most similar to the first, in terms of calling, because it takes a function.

ssamuel68
14 Sep 2013, 2:59 PM
Well, on the benchmark page that I found (http://jsperf.com/fors-vs-while/58) when I run all the test the forEach statement is the slower one, so im looking the more efficient function for looping.

Im not really sure that the Ext.each is the equivalent for the Array.forEach

evant
14 Sep 2013, 3:37 PM
Writing a for loop will typically always be faster than using each/forEach, because there's an extra overhead involved in function invocation.

However I doubt it will ever matter, unless the code is being called a huge amount of times in a performance critical part of your app, you're much better off using each/forEach because it is (IMO) easier to read and less prone to errors (less moving parts).

Ext.each is mapped to Ext.Array.forEach, which basically just does a for loop over the collection. You can check out the source yourself.