Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Ext.forEach

  1. #1
    Sencha Premium User
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    2,377
    Vote Rating
    720
      -1  

    Default 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 User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,050
    Vote Rating
    1381
      -1  

    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 @LikelyMitch
    Modus Create, Senior Frontend Engineer
    ________________
    Need any sort of Ext JS help? Modus Create is here to help!

    Check out my GitHub:
    https://github.com/mitchellsimoens

  3. #3
    Sencha Premium User
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    2,377
    Vote Rating
    720
      -2  

    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 User evant's Avatar
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    18,984
    Vote Rating
    933
      0  

    Default

    Ideally, yes. Practically, not quite there yet! http://triin.net/2011/03/02/Optimizi...h:_Not_so_fast!
    Twitter - @evantrimboli
    Former Sencha framework engineer, available for consulting.
    As of 2017-09-22 I am not employed by Sencha, all subsequent posts are my own and do not represent Sencha in any way.

  5. #5
    Sencha Premium User
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    2,377
    Vote Rating
    720
      -2  

    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 User mschwartz's Avatar
    Join Date
    Nov 2008
    Location
    San Diego, Peoples' Republic of California
    Posts
    2,061
    Vote Rating
    19
      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
    Sencha Premium User
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    2,377
    Vote Rating
    720
      -2  

    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 User mschwartz's Avatar
    Join Date
    Nov 2008
    Location
    San Diego, Peoples' Republic of California
    Posts
    2,061
    Vote Rating
    19
      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 User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,050
    Vote Rating
    1381
      -2  

    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 @LikelyMitch
    Modus Create, Senior Frontend Engineer
    ________________
    Need any sort of Ext JS help? Modus Create is here to help!

    Check out my GitHub:
    https://github.com/mitchellsimoens

  10. #10
    Sencha Premium User
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    2,377
    Vote Rating
    720
      -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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •