0


I know the Browser Javascript Stack size limit, but is there any way to break it? while I am reading Javascript Stack size limit in net, I can not find a way to overcome the browser Stack size limit. Is there any tricks to do it in java-script or angular.

for example

var i=0;
rec(para);
function rec(para){
          if(para>=50000){

          }
          else{
            $scope.items.push(i);
            rec(i++)
          }

    }

is it possible to add 50000 data into an array..

jasonleonhard
  • 12,047
  • 89
  • 66
nisar
  • 1,055
  • 2
  • 11
  • 26
  • 3
    what exactly do you want to achieve? Can you share your specific usecase? – gurvinder372 May 03 '16 at 09:58
  • 2
    Why do you want to push 50,000 items in your array recursively? Why not doing it iteratively? – Frédéric Hamidi May 03 '16 at 10:01
  • A simple `for` loop will solve this, no need to do that recursively. If you must you can call function inside setTimeout (setImmediate for node) to clear out stack. – CKK May 03 '16 at 10:02
  • difff b/w both recursively and iteratively? from my understand both are same..recursively is speeder than iteratively. – nisar May 03 '16 at 10:02
  • 1
    50000 recursive calls will break most any stack. Languages that let you write stuff like this actually perform tail call optimization and turn recursion into iteration behind the scenes (Haskell, Erlang and such). As for using "asynchronous recursion" as suggested below, it's OK to skim across a list of pending requests, but it'll be unbelievably slow to fill up a simple array like this. – Mathias Dolidon May 03 '16 at 10:04
  • `is it possible to add 50000 data into an array..` yes, by iteration. You need to tell us where you are stuck so that we can help you with it. – gurvinder372 May 03 '16 at 10:05
  • 2
    *"recursively is speeder than iteratively."* - That seems highly unlikely. Why do you think that? – nnnnnn May 03 '16 at 10:15
  • @nnnnnn I have some experience in canvas for creating graphs, we always use recursive not loops(iteratively),iteratively takes time and some time it does not reponse... – nisar May 03 '16 at 10:18

2 Answers2

1

Use asynchronous means

function func1(n, callback)
{
   //do some work

   //check for exit condition
   if (n == 1)
   {
     callback();// invoke callback handler and pass the results to it.
   }
   else
   {
      setTimeout(function(){
         func1(); //call it again with modified parameters
      });
   }
}

One downside is that you won't be able to return the value from this method anymore.

is it possible to add 50000 data into an array..

Yes, by Iteration. In your case, you simply need to do

function rec()
{
  for( var counter = 0; counter < 50000; counter++ )
  { 
    $scope.items.push(counter);
  }
}
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • recursive is coming but it takes some times, but iteration takes so long time. can we reduce a time? – nisar May 03 '16 at 10:16
  • @nisar I doubt that a simple iteration like I have shown above will take so much time. You need to share your actual problem so that we can help you with more specific diagnosis and answer. – gurvinder372 May 03 '16 at 10:18
  • just, tried one POC to check how many data we can handle in js...becoz we are going to handle one large no of DB, that returns JSON (more than 50k val)our back end is java ,We are calling API to return JSON. I planning to do with scroll. – nisar May 03 '16 at 10:19
  • @nisar That would also depend on your system's infra. But the solution may vary from context to context. – gurvinder372 May 03 '16 at 10:22
0

If your environments allows it, try to stick with native ES5/6 methods. You can add huge consecutive sequence of numbers into array using fill and map in a single step:

// assuming $scope.items is already initialized array
$scope.items = $scope.items.concat((Array(50000).fill(0).map(function(e,i){return i})));

http://www.2ality.com/2013/11/initializing-arrays.html, Array.fill, Array.map.

myf
  • 9,874
  • 2
  • 37
  • 49