Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to optimize the tail call in web

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how to optimize the tail call in web", the content is simple and clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to optimize the tail call in web" this article.

Tail call

Literally, it naturally returns a call to a function at the end of the function, which generally refers to the last step in the execution of the function.

For instance

Const fn = () = > F1 () | | f2 () / / here, the f2 function may be a tail call, and F1 cannot be a tail call.

Why isn't the F1 function? let's look at the equivalent form of this function.

Const fn = function () {const flag = F1 () if (flag) {return flag} else {return f2 ()}}

It seems that at this point, according to the definition of tail call, we understand that only the f2 function is called at the tail.

At this point, why do you say tail call? Let's think about traditional recursion in advance. typically, we first perform a recursive call, and then settle the result according to the return value of this recursion. What are the disadvantages of traditional recursion?

Inefficient and takes up memory.

If the recursive chain is too long, it may stack overflow

So can we do optimization? this can involve the tail call mentioned above. What is its principle?

According to teacher Ruan Yifeng's explanation in es6's function expansion, the function call will form a "call record" in memory, also known as "call frame" (call frame), saving information such as the location of the call and internal variables. If function B is called inside function A, a call frame of B is also formed above the call frame of function A. When B finishes running, the calling frame that returns the result to Agraine B disappears. If function C is also called inside function B, there is also a call frame for C, and so on. All the call frames form a "call call stack".

The "call frame" and "call stack" here should mean "execution environment" and "call stack". Because of the last operation of the function at the end of the call, it is no longer necessary to retain the outer calling frame, but to directly replace the outer calling frame, so it can play an optimization role.

From the above description, we can understand that

Its principle is similar to when the compiler detects that a function call is tail recursive, it overwrites the current active record instead of creating a new call record in the function stack.

In this way, we can also understand that different language compilers or interpreters have done tail recursive optimization so that it will not burst the stack.

Since this is the case, the optimization of tail recursion depends on the browser, so which mainstream browsers support it?

Safari and Firefox, if you are interested, you can learn about it and write a Fibonacci series to verify it.

Manual optimization

Now that we know that there are not many browsers that support tail recursion optimization, you will be curious that when we use tail recursion for optimization, there is still a stack overflow error, so how do we solve it?

I saw a good scheme on the Internet, using the trampoline function.

Function trampoline (f) {while (f & & f instanceof Function) {f = f ();} return f;}

So how to use it?

Let's take the most common Fibonacci series.

Function fibonacci (n) {if (n = = 0) return 0 if (n = 1) return 1 return fibonacci (n-1) + fibonacci (n-2)}

According to the above formula, we can write it in an iterative form and cache its value with a variable.

Function fibonacci (n, ac1 = 0, ac2 = 1) {return n is all the content of the article "how to optimize the tail call in web". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report