blog's People
blog's Issues
记一次 JavaScript 栈溢出的 debug 过程
遇到栈溢出异常的第一反应是递归调用栈爆了,但是在 JavaScript 这门几乎还没有尾递归优化(“几乎”是因为有少量运行环境已经支持)的语言中,我很少会用递归
排除递归之后,考虑是不是调用链过长导致栈爆了,因为我会习惯用一些高阶函数层层嵌套。于是老老实实改写了一些代码为 for 循环,加了一些临时变量存中间结果,栈确实就不爆了。但是始终没有定义到问题所在,先交了代码下班了
第二天单步调试(话说,单步不会报异常,到了异常那一步直接页面 crash 了)了一会儿,发现了真实的问题所在,在求一个数组的最大最小值的时候,用了 Math.min/max 和 apply 方法,即
Math.min.apply(null, arr)
但是这个数组非常大,而 Math.min/max 实际上接受的是多个参数,参数过多导致栈溢出了。(这个时候脑海中浮现出了操作系统课上那幅标着各个寄存器的 stack frame 图,又想起上次遇到的 inode 问题,后悔操作系统没有好好学)
参考: 使用 apply 和内置函数
PS:聪明的 apply 用法 -> “聪明反被聪明误”
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.