replace
This commit is contained in:
parent
e2443e8de6
commit
75e30539ef
73 changed files with 195 additions and 195 deletions
|
@ -37,4 +37,4 @@ P.S. Naturally, the formula is the fastest solution. It uses only 3 operations f
|
|||
|
||||
The loop variant is the second in terms of speed. In both the recursive and the loop variant we sum the same numbers. But the recursion involves nested calls and execution stack management. That also takes resources, so it's slower.
|
||||
|
||||
P.P.S. The standard describes a "tail call" optimization: if the recursive call is the very last one in the function (like in `sumTo` above), then the outer function will not need to resume the execution and we don't need to remember its execution context. In that case `sumTo(100000)` is countable. But if your Javascript engine does not support it, there will be an error: maximum stack size exceeded, because there's usually a limitation on the total stack size.
|
||||
P.P.S. The standard describes a "tail call" optimization: if the recursive call is the very last one in the function (like in `sumTo` above), then the outer function will not need to resume the execution and we don't need to remember its execution context. In that case `sumTo(100000)` is countable. But if your JavaScript engine does not support it, there will be an error: maximum stack size exceeded, because there's usually a limitation on the total stack size.
|
||||
|
|
|
@ -98,7 +98,7 @@ function pow(x, n) {
|
|||
|
||||
The maximal number of nested calls (including the first one) is called *recursion depth*. In our case, it there will be exactly `n`.
|
||||
|
||||
The maximal recursion depth is limited by Javascript engine. We can make sure about 10000, some engines allow more, but 100000 is probably out of limit for the majority of them. There are automatic optimizations that help to alleviate this ("tail calls optimizations"), but they are not yet supported everywhere and work only in simple cases.
|
||||
The maximal recursion depth is limited by JavaScript engine. We can make sure about 10000, some engines allow more, but 100000 is probably out of limit for the majority of them. There are automatic optimizations that help to alleviate this ("tail calls optimizations"), but they are not yet supported everywhere and work only in simple cases.
|
||||
|
||||
That limits the application of recursion, but it still remains very wide. There are many tasks where recursive way of thinking gives simpler code, easier to maintain.
|
||||
|
||||
|
@ -164,7 +164,7 @@ To calculate `x*pow(x, n-1)`, we need to make a subcall of `pow` with new argume
|
|||
|
||||
### pow(2, 2)
|
||||
|
||||
To do a nested call, Javascript remembers the current execution context in the *execution context stack*.
|
||||
To do a nested call, JavaScript remembers the current execution context in the *execution context stack*.
|
||||
|
||||
Here we call the same function `pow`, but it absolutely doesn't matter. The process is the same for all functions:
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue