By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,470 Members | 924 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,470 IT Pros & Developers. It's quick & easy.

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

P: 1
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?

Expand|Select|Wrap|Line Numbers
  1. var list = readHugeList();
  2.  
  3. var nextListItem = function() {
  4.     var item = list.pop();
  5.  
  6.     if (item) {
  7.         // process the list item...
  8.         nextListItem();
  9.     }
  10. };
Nov 10 '15 #1
Share this Question
Share on Google+
1 Reply


Dormilich
Expert Mod 5K+
P: 8,639
the only way to retain the recursive code is to use a JavaScript engine with proper Tail Call Optimisation.
Nov 10 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.