The following recursive code will cause a stack overflow if the array list is too lar

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jobin petyer
    New Member
    • Nov 2015
    • 1

    The following recursive code will cause a stack overflow if the array list is too lar

    The following recursive code will cause a stack overflow if the array list is too large. How can you fix this and still retain the recursive pattern?

    Code:
    var list = readHugeList();
    
    var nextListItem = function() {
        var item = list.pop();
    
        if (item) {
            // process the list item...
            nextListItem();
        }
    };
    Last edited by Dormilich; Nov 10 '15, 08:53 AM. Reason: please use code tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the only way to retain the recursive code is to use a JavaScript engine with proper Tail Call Optimisation.

    Comment

    Working...