1. #1
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,299
    Vote Rating
    109
    LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light

      0  

    Default Ext.forEach

    Ext.forEach


    I noticed that Ext.each is used very frequently. It's a very convenient and flexible way to iterate, but it is also slow.

    I suggest adding this mapping and using Ext.forEach instead of Ext.each.

    Code:
    Ext.forEach = ExtArray.forEach
    forEach is faster because it maps (in newer browsers) to native forEach. I checked the Ext code base... in many (if not almost all) places each could be replaced by faster forEach because we don't check the return value of the callback.

    This shortcut would also save a few bytes because it could replace Ext.Array.forEach in a number of places

  2. #2
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,643
    Vote Rating
    816
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      0  

    Default


    Ext.each is also slow, executing a function for every array/object iteration ("function iteration") comes at a cost and we are looking at removing it all internally. I have already gone through and done this but it's a massive change that needs to be tested and performance analyzed.
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  3. #3
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,299
    Vote Rating
    109
    LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light

      0  

    Default


    Quote Originally Posted by mitchellsimoens View Post
    Ext.each is also slow, executing a function for every array/object iteration ("function iteration") comes at a cost and we are looking at removing it all internally. I have already gone through and done this but it's a massive change that needs to be tested and performance analyzed.
    I found a post that disagrees with this approach...

    We really shouldn't put our efforts into doing that kind of micro-optimizations by hand. Instead we should build a JavaScript compiler that would do that kind of transformations for us.

  4. #4
    Sencha - Ext JS Dev Team evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    16,663
    Vote Rating
    584
    evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute evant has a reputation beyond repute

      0  

    Default


    Ideally, yes. Practically, not quite there yet! http://triin.net/2011/03/02/Optimizi...h:_Not_so_fast!
    Evan Trimboli
    Sencha Developer
    Twitter - @evantrimboli
    Don't be afraid of the source code!

  5. #5
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,299
    Vote Rating
    109
    LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light

      0  

    Default


    Quote Originally Posted by mitchellsimoens View Post
    Ext.each is also slow, executing a function for every array/object iteration ("function iteration") comes at a cost and we are looking at removing it all internally. I have already gone through and done this but it's a massive change that needs to be tested and performance analyzed.
    I found an Ext.each that should be converted to a plain for loop.

    Code:
    Ext.define('Ext.util.AbstractMixedCollection', {
        ...
        removeAll : function(items){
            Ext.each(items || [], function(item) {
                this.remove(item);
            }, this);
    
            return this;
        },
        ...

  6. #6
    Sencha - Community Support Team mschwartz's Avatar
    Join Date
    Nov 2008
    Location
    San Diego, Peoples' Republic of California
    Posts
    2,053
    Vote Rating
    17
    mschwartz will become famous soon enough mschwartz will become famous soon enough

      0  

    Default


    If the 90/10 rule is really true, then 90% of these loops should be Ext.each() for maintainability and readability.


  7. #7
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,299
    Vote Rating
    109
    LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light

      0  

    Default


    Quote Originally Posted by mschwartz View Post
    If the 90/10 rule is really true, then 90% of these loops should be Ext.each() for maintainability and readability.

    You got this half right.

    These loops should be Ext.forEach, not Ext.each.

    Ext.forEach is much faster compared to Ext.each

  8. #8
    Sencha - Community Support Team mschwartz's Avatar
    Join Date
    Nov 2008
    Location
    San Diego, Peoples' Republic of California
    Posts
    2,053
    Vote Rating
    17
    mschwartz will become famous soon enough mschwartz will become famous soon enough

      0  

    Default


    I'm not an Ext4 user yet, though I've dabbled a little.

    Ideally, a forEach() type function should work on both arrays and objects.

    For example:

    Code:
    var a = [ 1,2,3];
    forEach(a, function(value, key) {
      console.log(value);
    });
    // => 
    // 1
    // 2
    // 3
    
    var o = { foo: 'bar', baz: 'zaz' };
    forEach(o, function(value, key) {
      console.log(key + ' = ' + value);
    });
    // => 
    // foo = bar
    // baz = zaz

  9. #9
    Sencha - Senior Forum Manager mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    36,643
    Vote Rating
    816
    mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute mitchellsimoens has a reputation beyond repute

      -1  

    Default


    I personally wouldn't use Ext.each... Ext.forEach... store.each...

    They all are going to execute a function for every iteration that is unneeded. Why not just do a for loop where possible?
    Mitchell Simoens @SenchaMitch
    Sencha Inc, Senior Forum Manager
    ________________
    Check out my GitHub, lots of nice things for Ext JS 4 and Sencha Touch 2
    https://github.com/mitchellsimoens

    Think my support is good? Get more personalized support via a support subscription. https://www.sencha.com/store/

    Need more help with your app? Hire Sencha Services services@sencha.com

    Want to learn Sencha Touch 2? Check out Sencha Touch in Action that is in print!

    When posting code, please use BBCode's CODE tags.

  10. #10
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,299
    Vote Rating
    109
    LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light LesJ is a glorious beacon of light

      1  

    Default


    Quote Originally Posted by mitchellsimoens View Post
    I personally wouldn't use Ext.each... Ext.forEach... store.each...

    They all are going to execute a function for every iteration that is unneeded. Why not just do a for loop where possible?
    Performance-wise:

    for loop > Ext.forEach > Ext.each

    But, for loops are more prone to bugs and the code is harder to understand and maintain.

    I'd use for loops only in performance critical code.