On Mon, 19 Mar 2012 06:23:13 -0700, Krinkle krinklemail@gmail.com wrote:
<offtopic>
On Mon, Mar 19, 2012 at 9:35 AM, Daniel Friesen lists@nadir-seen-fire.comwrote:
On Mon, 19 Mar 2012 00:40:54 -0700, Dmitriy Sintsov questpc@rambler.ru wrote: var jqgmap = [];
for ( var mapIndex in jqgmap ) {
This is VERY bad JavaScript coding practice. Please use $.each().
This is rather exaggerated. Even more when looking at that suggestion.
There's nothing exaggerated about telling people not to use for..in to loop over arrays. for..in is object iteration, period.
The $.each recommendation is because that IS our equivalent to foreach for arrays. Don't underestimate the developer distaste for writing stuff like this all the time: `for ( var i = 0, l = arr.length; i < l; i++ ) { var arr2 = arr[i]; for ( var j = 0, ll = arr2.length; j < ll; j++ ) { var item = arr2[j]; } }`
I never said not to use numeric for loops. But there's no reason to ditch clean code.
Arrays should, indeed, not be enumerated with a for-in loop. Arrays in JS can only contain numeral indices, so they should simply be iterated with a simple for-loop like this `for (i = 0; i < myArr.length; i += 1) { .. }` (maybe cache length for slight performance gain by reducing property lookups).
Using $.each has overhead (+1+n function invocations). When given an array it will do a simple for-loop with a counter. It has overhead of 1+n additional function invocations and context creations. In most cases there is no use for it. There is one case where it is handy and that's when you specifically need a local context for the loop (being careful not to create later-called functions inside the loop, risking all variables being in the post-loop state). If you don't need a local scope for your loop, then using $.each (or the native [].forEach in later browsers) is pointless as it only has overhead of additional function invocations and lowering the position in the scope chain.
When iterating over objects, however, (not arrays) then $.each is no better than a for-in loop because (contrary to what some people think) it is not a shortcut for for-in + if-hasOwn wrapper. When an object is passed, it literally just does a plain for-in loop invoking the callback with the value. jQuery does not support environments where someone extends the native Object.prototype because it is considered harmful (an therefore MediaWiki inherently does not support that either), so a plain for-in loop over an object (excluding array objects) is perfectly fine according to our conventions.
See also http://stackoverflow.com/a/1198447/319266
.... but so much for the good (and bad, evil) parts of javascript :D
-- Krinkle
Micro-optimizations like these are pointless. Most arrays have under 100 elements. No matter what type of loop you use the time taken for the operation is negligible. More important is that the code is clean and easy to read and write.
Unless you are working with 5000+ element arrays or working with really low level code doing things like parsing and highlighting code there's no real reason to micro-optimize.
If we cared about efficiency at that level. We wouldn't be using any of jQuery's dom querying or dom creation functionality. dom operations are practically the slowest thing there is to work with and jQuery just adds overhead.
Btw, ;) for..in is actually the least efficient way to loop, the only thing that is slower than for..in is for..in with hasOwnProperty: http://jsperf.com/loop-testx http://jsperf.com/loop-test-short