1. #1
    Sencha User
    Join Date
    Sep 2011
    Location
    Taiwan
    Posts
    74
    Vote Rating
    0
    cwtuan is an unknown quantity at this point

      0  

    Default Performance of Ext.Array.each is better?

    Performance of Ext.Array.each is better?


    According to http://www.sencha.com/blog/ext-js-4-1-performance, it suggests using for (...) rather than Ext.Array.each.
    But, in my experiment, I am so surprised that Ext.Array.each has better performance than for (...). Why?

    PHP Code:
            var = [];        
            for ( var 
    0100000i++) {
                
    r.push(i);
            }

            
    time = new Date();
            for ( var 
    0r.lengthi++) {
            }
            
    console.log(new Date() - time); // 200 ms

            
    time = new Date();
            
    Ext.Array.each(r, function(rec) {
            });
            
    console.log(new Date() - time); // 55 ms 

  2. #2
    Sencha Premium Member skirtle's Avatar
    Join Date
    Oct 2010
    Location
    UK
    Posts
    3,590
    Vote Rating
    322
    skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future skirtle has a brilliant future

      0  

    Default


    Typing this directly into the console I saw the same thing. However, if I wrap it in a function the order switches.

    All sorts of possible reasons. Perhaps the JIT compiler can only work on functions. Or maybe it's because local variables are faster than globals. I haven't bothered digging any further.

    Try:

    Code:
    var fn = function() {
        var r = [], sum = 0, time;
    
        for (var i = 0 ; i < 100000 ; i++) {
            r.push(i);
        }
    
        time = new Date().getTime();
    
        Ext.Array.each(r, function(rec) {
            sum += rec;
        });
    
        console.log(sum, new Date().getTime() - time);
    
        sum = 0;
        time = new Date().getTime();
    
        for (var i = 0, len = r.length ; i < len ; i++) { // Note caching the length
            sum += r[i];
        }
    
        console.log(sum, new Date().getTime() - time);
    };
    
    fn();

Thread Participants: 1

Tags for this Thread