当前位置:网站首页>JS recursion and while
JS recursion and while
2022-06-25 14:19:00 【jason_ renyu】
As a senior coder in front-end development for many years , I have always been dismissive of heaps and stacks , At work while Rarely used , Use recursion instead , In a recent inadvertent calculation operation, I found that js The drawback of recursion in , Then I did an experiment myself :
Recursion for display
// recursive
let num = 0;
const max = 1000000;
function a () {
num++;
console.log(` Current number of cycles :${num}`);
if (num === max) {
return false;
} else {
return a();
}
}
setTimeout(() => {
a();
}, 100);result :

Display stack overflow , use while To operate :
let num = 0;
const max = 1000000;
// while loop
while (num !== max) {
num++;
console.log(` Current number of cycles :${num}`);
} One step at a time :
Later, I checked on the Internet ,Aleph Sir pointed out that , stay V8 in , The number of recursive calls depends on two quantities : Stack size and stack frame ( Save local variables of parameters ) Size . You can use the computeMaxCallStackSize() Add local variables for validation - It returns the low order value .
Use the following function to find out the answer :
function computeMaxCallStackSize() {
try {
return 1 + computeMaxCallStackSize();
} catch (e) {
// Call stack overflow
return 1;
}
}The three results :
Node.js: 11034
Firefox: 50994
Chrome: 10402I have seen other bloggers and had similar experiences , Some of the solutions given in this paper are the same , Use loops to bring recursion , Also have say es6 This aspect is optimized , But I haven't succeeded yet , If there is a great God, please give me some advice .
边栏推荐
- Deeply understand the mathematics behind deep neural networks (mysteries of neural networks Part I)
- 多台云服务器的 Kubernetes 集群搭建
- 程序员为什么要软一点?
- Gorm-- search you don't know
- Partager les points techniques de code et l'utilisation de logiciels pour la communication Multi - clients socket que vous utilisez habituellement
- mysql数据库压缩备份_Mysql备份压缩及恢复数据库方法总结
- K-line diagram 24 classic diagrams (shadow)
- 测一测你的挣钱能力有多强?未来的你将从事什么职业?
- Stream竟然还有应用进阶学习?作为程序员的你知道吗
- Logistic Regression VS Linear Regression
猜你喜欢

Variables, scopes, and variable promotion

k线图24种经典图解(影线篇)

当了六年程序员第一次搞懂微服务架构的数据一致性,真不容易

How does hash eagle, the founder of equity NFT, redefine NFT and use equity to enable long-term value?

Report on Hezhou air32f103cbt6 development board

Complete and detailed compilation of experimental reports

Classifier and cross entropy loss function

Today in history: Netease was founded; The first consumer electronics exhibition was held; The first webcast in the world

Summary of common functions in Oracle Database

Shell array
随机推荐
H5 page graying source code, ie compatible (elegant downgrade provides download browser link)
JS floating point multiplication and division method can not accurately calculate the problem
What are the red lines of open source that should not be trodden on?
Logistic Regression VS Linear Regression
Is it safe for tongdaxin to open a stock account
Renix Perf: IP网络性能测试工具及测试用例参数详解
shell 运算符
Nine parts of speech and nine tenses in English
Kubernetes cluster construction of multiple ECS
To make pytorch faster, you need to master these 17 methods
Is qiniu regular? Is it safe to open a stock account?
使用调试工具调试博图TCP连接所遇到的问题
广发易淘金和同花顺哪个更好,更安全一些
超酷汇编教程-- 简明x86汇编语言教程(1)
专家建议|8大措施加速你的创新职业规划和成长
请问通达信股票开户是安全的吗?
[untitled]
How to choose a technology stack for web applications in 2022
Classifier and cross entropy loss function
Deconstruction assignment of variables