当前位置:网站首页>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
direct
As 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
边栏推荐
- How to import Excel data in SQL server, 2019 Edition
- Talk about the essence of interface idempotent and consumption idempotent
- Distributed service governance Nacos
- 数据库之MySQL——基础篇
- NLLLoss+log_SoftMax=CE_Loss
- 【编程实践/嵌入式比赛】嵌入式比赛学习记录(一):TCP服务器和web界面的建立
- 聊聊接口幂等与消费幂等的本质
- LeetCode 1611. 使整数变为 0 的最少操作次数
- 数据库之Mysql——概述安装篇
- Reverse linked list exercise
猜你喜欢
Distributed service governance Nacos
为什么会存在1px问题?怎么解决?
简述CPU
【Appium】测试时遇到手机内嵌H5页面的切换问题
Intranet penetration series: dnscat2 of Intranet tunnel
Comparison of indoor positioning methods of several intelligent robots
MySQL -- the secret of lock -- how to lock data
Upload labs range practice
巨头押注的全屋智能,正在驱动海信、华为、小米们「自我革命」
Smart business card applet business card details page function implementation key code
随机推荐
Interview learning route
数据库之Mysql——概述安装篇
几种智能机器人室内定位方法对比
Mobile terminal layout (3D conversion, animation)
BUUCTF MISC刷題
Redis transaction implements optimistic locking principle
Asynchronous learning
【Appium】测试时遇到手机内嵌H5页面的切换问题
利用sqlmap注入获取网址管理员账号密码
使用 Ingress 实现金丝雀发布
How to import Excel data in SQL server, 2019 Edition
在线YAML转XML工具
NFT ecological development of Ignis public chain: unicorn Donation and development of Art
数据库之MySQL——基础篇
Go语学习笔记 - 数组 | 从零开始Go语言
Mobile web (Font Icon, plane conversion, color gradient)
Cloud computing skills competition -- Part 2 of openstack private cloud environment
C 输出一种二维数组,特点如下。
Analysis of Nacos source code
Research on system and software security (I)