5

Is there anybody would explain why the result is different below?

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

console.log(computeMaxCallStackSize()); 

result is 17958

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

    console.log(computeMaxCallStackSize());

result is 15714

When the position of the function 'computeMaxCallStackSize' is different,the result is different too. What's the reason? Thanks very much!

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

4
  • probably a bug in an earlier version of v8, on 7.3.0 darwin x64 results are the same. Commented Apr 11, 2017 at 8:43
  • Is that true?I think I'll try it later. Commented Apr 11, 2017 at 9:20
  • Have you tried flipping the two test cases? Often enough it's not about the code, but whether it was executed first. Commented Apr 11, 2017 at 10:25
  • Actually,I tried it on Linux x86.I still got different result. Commented Apr 12, 2017 at 3:33

1 Answer 1

2

Its not the position but the order of execution which leads to this in the first function the statement

 return computeMaxCallStackSize() + 1;

calls the increment first and then adds 1

return 1 + computeMaxCallStackSize();

If you try both return statements as same then it leads to same value. In later one as digit is first the js call stack exceeds overflow sooner as compared to first. The callstack value depends on the order of execution as in 2nd you change the order you get a lower value as recursion happens later.

You can also check by adding some console.log() or local variable call stack will decrease gradually with increase in execution statements.

If you try computeMaxCallStackSize() + 1; in both you will get same value.

Sign up to request clarification or add additional context in comments.

6 Comments

Not sure what you mean by "calls the increment first and then adds 1" - both snippets do call computeMaxCallStackSize first and then add 1 to the value?
Yes but the execution order differs when you say function is called first that mean the commands on the callable functions are first pushed into stack you can check the order of execution in loupe
Execution order of what specifically? Which "commands on the callable functions" are pushed to the stack exactly? Usually commands aren't pushed to the stack at all, only state is. And what is loupe?
for return computeMaxCallStackSize() + 1; the function directly jumps to recursion leaving behind the addition factor because that would be resolved while returning recursion values whereas in second case it has to store 1 so that when recursion value is resolved it needs to evaluate you can try using njstrace(npmjs.com/package/njstrace) to check the execution its output is huge i will try to leave an accurate ans soon just putting my observations in order
Ah, you mean that the second one needs to evaluate the expression 1 and also keep its result value on the stack, yeah. Not sure whether the optimiser wouldn't boil that away, though (and it seems that njstrace will affect the possible optimisation).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.