在PhantomJS中,函数调用堆栈限制是多少? [英] In PhantomJS, what is the function call stack limit?

查看:104
本文介绍了在PhantomJS中,函数调用堆栈限制是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在减少PhantomJS javascript引擎的调用堆栈限制之前,一个函数可以递归调用几次?换句话说,这里为PhantomJS打印的最后一个n是什么:

How many times can a function recursively call itself before breeching PhantomJS javascript engine's call stack limit? Said another way, what is the last possible n printed here for PhantomJS:

var n = 0;
function f() {
  console.log(++n);
  f();
}
f();

推荐答案

我使用了您的代码,并在PC和运行Raspbian的Raspberry Pi 1上的不同PhantomJS版本中运行了它.

I've used your code and ran it in different PhantomJS versions on my PC and my Raspberry Pi 1 running Raspbian.


Platform | Version | Maximum callstack
--------------------------------------
Win 8.1  | 2.0.0   | 65277
Win 8.1  | 1.9.8   | 65534
Win 8.1  | 1.9.7   | 65534
Win 8.1  | 1.9.0   | 65534
Win 8.1  | 1.8.2   | 65534
RPi 1    | 2.0.1*  | 43547
RPi 1    | 1.9.7   | 65534
RPi 1    | 1.9.0   | 65534

*开发版本已于2015年3月13日在Raspberry Pi 2上编译

* Development version compiled on March 13 2015 on a Raspberry Pi 2

以下是一个更实际的代码示例,因为您很少在PhantomJS脚本或页面上使用同步和递归代码.

The following is a more realistic code example, because you're rarely using synchronous and recursive code in a PhantomJS script or on the page.

var n = 0;
function f() {
  console.log(++n);
  //f();
  setTimeout(f, 0);
}
f();

此异步版本(更可能使用)没有明显的调用堆栈限制.在大约300,000次迭代(52分钟)后,我停止了该过程(在Win 8.1上为v1.9.8和v2.0.0).版本1.9.8始终位于27.2 MB的内存中,而v2.0.0则在8至10 MB的内存范围内跳跃.

This asynchronous version, which is more likely to be used, has no apparent callstack limit. I stopped the process (v1.9.8 and v2.0.0 on Win 8.1) after about 300,000 iterations (52 minutes). Version 1.9.8 constantly sat at 27.2 MB of memory and v2.0.0 jumped around in the range of 8 to 10 MB of memory.

这篇关于在PhantomJS中,函数调用堆栈限制是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆