Are any JavaScript engines tail call (TCO) optimized?

后端 未结 6 1326
有刺的猬
有刺的猬 2020-11-27 03:02

I have a tail recursive pathfinding algorithm that I\'ve implemented in JavaScript and would like to know if any (all?) browsers would possibly get stack overflow exceptions

6条回答
  •  眼角桃花
    2020-11-27 03:42

    Currently no JavaScript implementations recognise tail recursion. Changes are being made in ECMAScript 6, and as others have said, there is an open ticket on V8.

    Here you can see V8's generated assembler for a tail recursion function:

    Example of how V8 compiles recursion

    Compare that to how Clang has compiled the same function in C

    Example of C compiler tail recursion

    V8 retains the recursive call, whereas the C compiler has recognised the tail recursion and changed it into a loop.

提交回复
热议问题