当前位置:网站首页>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 .
边栏推荐
- Variables, scopes, and variable promotion
- 英语中的九大词性与九大时态
- None of the MLIR optimization passes are enabled (registered 2) solutions
- 电脑必须打开的设置
- Share the code technology points and software usage of socket multi client communication
- [world history] Episode II: Dawn of civilization
- It's not easy to understand the data consistency of the microservice architecture for the first time after six years as a programmer
- Discriminative v.s.Generative
- Laravel8 implementation of picture verification code
- Realization of neural networks with numpy
猜你喜欢

合宙Air32F103CBT6开发板上手报告

解决报错:Creating window glfw ERROR: GLEW initalization error: Missing GL version

一次性讲清楚 Handler 可能导致的内存泄漏和解决办法 | 开发者说·DTalk

Nr-arfcn and channel grid, synchronous grid and GSCN

权益NFT开创者Hash Eagle如何重新定义NFT,用权益赋能长续价值?

Numpy库使用入门

Data acquisition system gateway acquisition plant efficiency

Sigmoid function sigmoid derivation

Preventing overfitting of deep neural networks (mysteries of neural networks Part II)

Logistic Regression VS Linear Regression
随机推荐
Garbage collection mechanism
Partager les points techniques de code et l'utilisation de logiciels pour la communication Multi - clients socket que vous utilisez habituellement
Websocket -- reverse proxy to solve cross domain problems
Syntax 'trap'
Explain the possible memory leaks caused by the handler at one time and the solutions | the developer said · dtalk
多臺雲服務器的 Kubernetes 集群搭建
通达信股票账户开户安全吗
Is it safe for tongdaxin to open a stock account
Two methods to rollback the code in pycharm to the specified version (with screenshot)
Les neuf caractéristiques de la parole et les neuf temps en anglais
启牛是正规的吗?股票开户安全吗?
Which is better and safer, GF easy gold rush or flush
[open source Hongmeng system display] the rk3568 development board is equipped with openharmony 3.1 release
阻止深度神经网络过拟合(Mysteries of Neural Networks Part II)
'NVIDIA SMI' is not an internal or external command, nor is it a runnable program or batch file
Deconstruction assignment of variables
golang项目依赖管理工具go vendor,go mod
Where can the brightness of win7 display screen be adjusted
Experts' suggestions | 8 measures to accelerate your innovative career planning and growth
Windows下MySQL的安装和删除