reverse do while

프로그래밍 2007. 5. 17. 02:01
전에 포스팅한 내용을 바탕으로 each문을 for문이 아닌 reverse do while으로 만들어봤습니다.
근데 생각보다 그렇게 많은 효과가 없습니다.
일반 for문과 비교하여 아주 약간 빠릅니다.

do while
Array.prototype.each = function(iterator){
        var size = this.length-1;
        if(size>-1){
            do{
                iterator(size,this[size]);
            }while(size--);
        }
};

for
Array.prototype.each = function(iterator){
        var size = this.length;
        for(var i=0;i<size;i++){
            iterator(i,this[i]);
        }
};

이유는 배열이기 때문에 그렇습니다.
확인 해볼결과 배열은 스택으로 구현되어있습니다.;;
Posted by 전용우
,