当前位置:网站首页>Discussion on ES6 tail tune optimization
Discussion on ES6 tail tune optimization
2022-04-23 08:10:00 【fernwehseven】
Preface
I met this problem during the written examination two days ago , After reading many blog posts, I finally understand , Record it here ~
What is a tail call
Simply speaking , The return value of a function is a call to another function , This situation is called tail call .
function f1(n) {
return n * 2
}
function f2(n) {
return f1(n + 1) //line A
}
const res=f2(2))//line B
In the above code ,f2 The return value of is right f1 Call to , This is it. Tail call .
Optimization of tail call
First of all, we need to know that tail tuning optimization is the work that the browser helps us complete , As long as certain conditions are met , This optimization is triggered .
We all know : At code execution time , A call stack will be generated , When a function is called, it is pushed onto the stack , When it return It will be out of the stack after .
Taking the above code as an example, we are right ES5 and ES6 Different treatment schemes are analyzed .
stay ES5 in :
- Call function f2,f2 Push , And record where it is called , In order to know the return value .
- The function called at the end f1 Will be pushed into a new stack frame to represent the call , Record where it is called on the stack . When you get the return value of this function ,f1 end of execution , Bomb stack .
- f2 Got it f1 The return value of ,f2 The execution of is over , Bomb stack .
You can see that in this call , Every stack frame that is not used up is kept in memory .
Observation can be seen ,f1 The execution result of does not have to reach lineA, But you can go directly to lineB. Why? ? because f2 The return value of f1 The return value of , No operation has been done on this basis .
stay ES6 in , Optimized for this situation .
stay ES6 in :
Reduce the size of tail call stack frame in strict mode , If the following conditions are met , No more new stack frames are created , Instead, empty and reuse the stack frame of the current function :
- The tail call no longer accesses the variable of the current stack frame ( Functions are not closures )
- The last call statement is on the last line of the function
- Result of tail call
directAs the return value of the function
How to understand direct Well ? For example, tail tune optimization will not be done in the following cases
function f1(n) {
return n * 2
}
function f2(n) {
// stay f2 Return... Not directly f1 Result , But to add one
return 1+f1(n + 1) //line A
}
const res=f2(2))//line B
The reason is well understood , because f1 The return value of cannot be reached directly lineB, But to arrive first lineA add 1.
Tail recursion
What is tail recursion ? In fact, it is a special form of tail call : When the tail call is a call to itself , Tail recursion .
const sum = (n) => {
if (n <= 1) return n;
return n + sum(n-1)
}
sum(5)
The purpose of this code is to calculate 1 To 5 And , But it is clear that this situation will not do tail tune optimization , Because the tail tone function is not direct return, But first n. When n When the number increases , Because the previous call stack cannot be released , Will cause stack overflow problem .
We can use ES6 The tail tone optimization feature of rewrites the above code :
const sum = (n, preSum = 0) => {
if (n <= 1) return n + preSum
else {
// current sum yes preSum+n, Recursion 1~n-1 Of sum
return sum(n - 1, preSum + n)
}
}
sum(5)
In this way, theoretically, there is no problem of maximum stack limit , But I'm actually testing ( use chrome) Is there a stack overflow problem found , After checking, it is said that some browsers do not support this optimization .fine~TAT
版权声明
本文为[fernwehseven]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230655426903.html
边栏推荐
- 巨头押注的全屋智能,正在驱动海信、华为、小米们「自我革命」
- Research on software security based on NLP (2)
- Mobile terminal layout (3D conversion, animation)
- 一个没啥L用,但可以装X的IDEA插件
- [go] common concurrency model [generic version]
- BUUCTF [极客大挑战 2019]EasySQL1
- 渗透测试面试合集---HVV---
- Anti shake and throttling
- Convert object to URL
- Asynchronous learning
猜你喜欢
随机推荐
vivo,硬件安全的爱与雷霆
Anti shake and throttling
Guoji Beisheng openstack container cloud environment construction
华硕笔记本电脑重装系统后不能读取usb,不能上网
Positioning and decoration style
BUUCTF MISC刷題
Comparison of indoor positioning methods of several intelligent robots
Go语学习笔记 - 结构体 | 从零开始Go语言
一篇文章看懂变量提升(hoisting)
浏览器中的 Kubernetes 和 IDE | 交互式学习平台Killercoda
[go] common concurrency model [generic version]
一个没啥L用,但可以装X的IDEA插件
NIH降血脂指南《your guide to lowering your Cholesterol with TLC》笔记(持续更新中)
【无标题】
LeetCode15. 三数之和
Planification du mouvement du manipulateur dans l'assemblage 3c
C outputs a two-dimensional array with the following characteristics.
Implementation of new
为什么会存在1px问题?怎么解决?
DVWA靶场练习









