On Thu, Jun 11, 2009 at 3:42 PM, Thomas Broyer <[email protected]> wrote:
> > On 11 juin, 16:11, John Tamplin <[email protected]> wrote: > > On Thu, Jun 11, 2009 at 7:51 AM, Joel Webber <[email protected]> wrote: > > > +1 Ray. Now here's the really tricky question. Is there any way we can > take > > > advantage of Javascript's "for (x in y) { ... }" syntax (and should we, > > > given its spotty performance)? My intuition tells me that the only way > we > > > could use it would be through a callback, because there's nothing like > .NET > > > generators/yield in Javascript. > > > I'm afraid I don't understand the relationship between JsArray/ > JsArrayList and for (x in y) { ... } Javascript arrays are sparse. There are use case (such as protocol buffers with extension fields) where this is relevant and for(x in y) may be faster. I agree we should avoid this in the general case, though. https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Statements/for...in > """Although it may be tempting to use this as a way to iterate over an > Array, this is a bad idea. The for...in statement iterates over user- > defined properties in addition to the array elements, so if you modify > the array's non-integer or non-positive properties (e.g. by adding a > "foo" property to it or even by adding a method or property to > Array.prototype), the for...in statement will return the name of your > user-defined properties in addition to the numeric indexes. Also, > because order of iteration is arbitrary, iterating over an array may > not visit elements in numeric order. Thus it is better to use a > traditional for loop with a numeric index when iterating over arrays. > Similar arguments might be used against even using for...in at all (at > least without propertyIsEnumerable() or hasOwnProperty() checks), > since it will also iterate over Object.prototype (which, though > usually discouraged, can, as in the case of Array.prototype, be > usefully extended by the user where are no namespacing concerns caused > by inclusion of other libraries which might not perform the above > checks on such iterations and where they are aware of the effect such > extension will have on their own use of iterators such as > for...in).""" > > And Nicholas C. Zakas recently confirmed that "for (var i=0, l = > arr.length; i < l; i++) { ... }" is the fastest way of iterating > through an array's values: > http://google-code-updates.blogspot.com/2009/06/nicholas-c-zakas-speed-up-your.html > (starts at slide #40) > (and this construct is the one generated by GWT from a Java "for (T > i : arr) { ... }", thus also with Ray's proposed "for (T i : > jsarr.elements()) { ... }"; but beware if the "jsarr" can be modified > --in length-- during the loop!) > > See also the toArray()s in > > http://code.google.com/p/gwt-in-the-air/source/browse/trunk/src/net/ltgt/gwt/jscollections/client/JsArrays.java > > Those methods (toArray(), elements()) are for people who really know > what they do, so I'm not sure they so should be included in the "core" > GWT... (the toJavaArray() from some Utils class in GALGWT always > copies the array, which is slower but always safe) > > > Regarding the for(x in y) construct, it seems conceivable the compiler > could > > recognize the code generated by the normal Java->JS compile of for(x : > > y.elements()) and rewrite it to the native JS code where appropriate as a > > peephole optimization. > > What the compiler could eventually do is optimize the "l=arr.length" > out of the for-loop when used several times in a row. E.g. > > void f(T[] arr) > { > this.sb = new StringBuilder(); > for (T i : arr) { this.sb.append(i.toString()); } > this.count = 0; > for (T i : arr) { this.count++; } > } > > to be compiled into (pseudo): > function f(arr) > { > var l = arr.length; > this.sb = new StringBuilder(); > for (var i = 0; i < l; i++) { this.sb.append(arr[i].toString()); } > this.count = 0; > for (var i = 0; i < l; i++) { this.count++; } > } > > > > -- Stefan Haustein Google UK Limited Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ; Registered in England Number: 3977902 --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---
