Javascript overflow overflow

Can anyone explain why the result is lower?

// test one function computeMaxCallStackSize() { try { return computeMaxCallStackSize() + 1; } catch (e) { return 1; } } console.log(computeMaxCallStackSize()); 

result 17958

 // test two function computeMaxCallStackSize() { try { return 1 + computeMaxCallStackSize(); } catch (e) { return 1; } } console.log(computeMaxCallStackSize()); 

result 15714

When the position of the 'computeMaxCallStackSize' function is different, the result is also different. What reason? Thank you so much!

Current environment: node.js v6.9.1 OS:Win7

+5
source share
1 answer

This is not a position, but an execution order that leads to this in the first function, a statement

  return computeMaxCallStackSize() + 1; 

first increment and then add 1

 return 1 + computeMaxCallStackSize(); 

If you try both return statements as the same, this will result in the same value. In the later case, when the digit is first, the js call stack exceeds the overflow sooner than before. The value of callstack depends on the order of execution, since in the second you change the order in which you get a lower value, since recursion occurs later.

You can also check by adding some console.log () or the stack stack of local variables will gradually decrease with increasing execution statements.

If you try computeMaxCallStackSize() + 1; , you will get the same value.

+2
source

Source: https://habr.com/ru/post/1266572/


All Articles