1. #1
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,329
    Vote Rating
    118
    LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all

      0  

    Default for vs array-foreach

    for vs array-foreach


    See this Ext.Array code fragment.

    According to jsPerf the most efficient way to iterate over an array or array-like collection is to avoid the native implementations entirely, opting for simple loops instead.

    http://jsperf.com/for-vs-array-foreach
    http://jsperf.com/for-vs-array-foreach/4


    Code:
    ...
    forEach: supportsForEach ? function(array, fn, scope) {
        return array.forEach(fn, scope);
    } : function(array, fn, scope) {
        var i = 0,
            ln = array.length;
    
    
        for (; i < ln; i++) {
            fn.call(scope, array[i], i, array);
        }
    },

  2. #2
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,329
    Vote Rating
    118
    LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all

      0  

    Default


    Quote Originally Posted by LesJ View Post
    See this Ext.Array code fragment.

    According to jsPerf the most efficient way to iterate over an array or array-like collection is to avoid the native implementations entirely, opting for simple loops instead.

    http://jsperf.com/for-vs-array-foreach
    http://jsperf.com/for-vs-array-foreach/4


    Code:
    ...
    forEach: supportsForEach ? function(array, fn, scope) {
        return array.forEach(fn, scope);
    } : function(array, fn, scope) {
        var i = 0,
            ln = array.length;
    
    
        for (; i < ln; i++) {
            fn.call(scope, array[i], i, array);
        }
    },
    https://github.com/documentcloud/und...mment-16981731

    This is not very intuitive that native methods can be slower....

    "Avoiding native iteration methods wins more than just performance, as stated above there's also gains in consistency, portability, and simplicity."

  3. #3
    Touch Premium Member
    Join Date
    Nov 2010
    Location
    Chicago
    Posts
    1,329
    Vote Rating
    118
    LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all LesJ is a name known to all

      0  

    Default


    Quote Originally Posted by LesJ View Post
    See this Ext.Array code fragment.

    According to jsPerf the most efficient way to iterate over an array or array-like collection is to avoid the native implementations entirely, opting for simple loops instead.

    http://jsperf.com/for-vs-array-foreach
    http://jsperf.com/for-vs-array-foreach/4


    Code:
    ...
    forEach: supportsForEach ? function(array, fn, scope) {
        return array.forEach(fn, scope);
    } : function(array, fn, scope) {
        var i = 0,
            ln = array.length;
    
    
        for (; i < ln; i++) {
            fn.call(scope, array[i], i, array);
        }
    },
    Hint:
    It would be better if we ditched the es5 array iterations.