Can your browser handle a call depth of 5000 functions? Look here and see how far you get with various JS engines:
I'd really like to know if there is a hard-coded limitation or if it depends on system speed and memory, for example. Discussion is open in the comments 🙂
I got to 1124 in IE6 and got an out of stack space error.
With Opera I get "function 4998 running"With firefox I get "- died when trying to call function 1000, error was "too much recursion""
In a fairly recent Firefox 3 build, the hard-coded 1000 limit mentioned earlier is no longer present and has been replaced with some amount of dynamic sizing (not complete, because if you actually let it expand to fill RAM you're swapping hard at the limit, hurting overall system performance). Using the following testcase, I get 228816 as the iteration depth:
,<script>%20var%20o;%20window.onload%20=%20function()%20{%20o%20=%20document.getElementById("out");%20recur(1);%20};%20function%20recur(n)%20{%20o.textContent%20=%20n;%20recur(n%20+%201);%20}%20</script>%20Iteration%20depth:%20<span%20id="out">(none,%20test%20failed%20to%20run)
Basically the depth is unlimited for all practical purposes, unless you're writing in continuation-passing style or you're using recursion instead of proper, unambiguous-performance looping constructs for iteration. (Can you guess what I think of languages which force iteration to be written to appear at the surface as stack-exhausting recursion? For any simple algorithm, the code to implement it shouldn't be required to look like it performs worse than it actually does.)
On Opera 9.5.9672 I get "function 4998 running." Does this mean it didn't get to function 5000?
Opera 9.50.1678 with 1.8gb ram here
"function 2503 running" on Opera 9.24.8816.0, 512MB RAM. Sometimes 2504."function 4998 running" on Opera 9.50.9665.0IE 6.0.2900.2180 reports '- died when trying to call function 1124, error was "Out of stack space"'Fx 2.0.0.10 reports: '- died when trying to call function 1000, error was "too much recursion"'Safari 3.0.3 reports: '- died when trying to call function 98, error was "Maximum call stack size exceeded."'PS: May I suggest uploading it as test.html.gz ?
Opera 9.24.8816.0 Iteration depth: 3341Opera 9.50.9665.0 Iteration depth: 9998FX 2.0.0.10 Iteration depth: 1000Safari 3.0.3 Iteration depth: 99By putting a "window.status = n" in front of "recur(n + 1)", it's possible to see the result in IE6, too: I get an iteration depth of 1124
Only 498 in Safari 3 on Windows :OError: "Maximum call stack size exceeded."But the smallest result I could get was under Adobe Air(uses WebKit, well, at least a very old version from mid 06 or so), after 98 calls it stopped.
On 9.24.8816 (Win Vista; 2 GB RAM): "function 2501 running" (~ between 2499 and 2502). IE 7: "- died when trying to call function 2548, error was "Nicht genügend Arbeitsspeicher."" (german for: "not enough memory").
Safari 3.0.4 (5523.10) on Mac OS X 10.5.1:- died when trying to call function 498, error was "Maximum call stack size exceeded."Opera 9.24 Build 3707 on Mac OS X 10.5.1:function 2498 running
4998 with Opera 9.5.9665 and 192MBOn a slightly similar note, try the TC in this bug to watch Safari and IE6 die, Opera and IE7 throw an exception and FF handle it just fine.
Just to complete the list of tested browsers:Firefox 3.0b2pre
Platform: Win32System: Windows XPJava: Sun Java Runtime Environment version 1.6TEST RESULTS, sorted by timeOpera 9.50 9665
Firefox 2.0.0.9
Netskape Navigator 9.0b1
Safari 3.0.3
MSIE 5.01
MSIE 5.5
MSIE 6
MSIE 7
Opera 9.5.9665 -function 4998 runningMinefield/3.0b2pre -function 5000 running – FINISHED! 5000 function calls took 423msOpera 9.24.8816 -function 2498 runningIE 7 — died when trying to call function 2550, error was "Out of memory"Windows XP/SP2 – 256MB Ram
On Fedora 7 (1GB RAM, ~1350 MHz Athlon):Opera 9.5 alpha: function 4998 runningFirefox 3 beta 1: completed all 5000.Epiphany (running Gecko 1.8.1.9, equiv. to Firefox 2.0.0.9): stopped at 1000 with "too much recursion"Konqueror 3.5.8: – died when trying to call function 998, error was "Exceeded maximum function call depth."On Windows XP (768 MB RAM, 2.93 GHz Celeron):Safari 3.0.4: – died when trying to call function 498, error was "Maximum call stack size exceeded"IE7: – died when trying to call function 2550, error was "Out of memory"No change on IE7 when I closed some more apps to free up memory.
2.2Mhz single core AMD Athlon 64, 2 GiB ram, winxpsp2:kestrel (b9624): function 4998 runningmerlin (9.24): function 2501 runningop 7.52 (b3808): function 1672 runningie6: – died when trying to call function 1124, error was "Out of stack space"saf 3.0.4b: – died when trying to call function 498, error was "Maximum call stack size exceeded."ff2.0.0.10: – died when trying to call function 1000, error was "too much recursion"ff3.0b1: function 5000 running – FINISHED! 5000 function calls took 264msUsing waldo's test:kestrel (b9624): Iteration depth: 9998saf 3.0.4b: Iteration depth: 499ff2.0.0.10: Iteration depth: 1000ff3.0b1: Iteration depth: 228816
Originally posted by Opera 9.50.9656:
Originally posted by Firefox 3 Beta 1:
Originally posted by IE7:
WinXP Pro SP2 384MB 2.2 GHzFirefox's average time was around 550ms after refreshing.just submitted http://browsershots.org/http://files.myopera.com/hallvors/blog/func5000.htmMS IE 6.0 on Windows 2000 crashed (oops… I didn't mean too!); died at 1124Safari 3.0.3 for Windows 2000 died at 98, call stack size exceeded.Firefox 2.0.0.9 for Windows 2000 died at 1000 – too much recursionOpera 9.24 for Windows XP – function 2504 running
Opera 9.5.1678 (x64)-First message was:"FAILED (seems stresstest script did not even parse.. though please wait a few seconds..)"-Then.."function 4998 running"..I hope we can reach the 5000 mark soon! Looks like we have some catching up to do (at least a little bit…ff3).Firefox 2.0.0.10 (x64)'- died when trying to call function 1000, error was "too much recursion"'Firefox 3 (Win32)"function 5000 running – FINISHED! 5000 function calls took 313ms"IE7 (Win32)' – died when trying to call function 2550, error was "Out of memory"'It looks like most are hard coded I guess. Opera 9.2x seems to be getting a few different results, while 9.5x is getting constants like IE and FF did.
"function 5000 running – FINISHED! 5000 function calls took 213ms"firefox 3b1 (amd x2 (s939) @2100mhz, 2gb ddr @230mhz 2-2-2-5 1T)others failed, seems to me clearly, that FF 3 does this test the best
Firefox 3b1 for Linux x86-32 went to 216556 for me.
Opera 9.20, Debian Linux: ~ 2500 recursions, like ct13 (above)
Ubuntu 7.10 (AMD Athlon64 3200+, 1GB RAM):- Opera 9.24: "function 2500 running"- opera-9.50-20071109.6-shared-qt.i386-1662: "function 4998 running"- Firefox 2.0.0.10: "- died when trying to call function 1000, error was "too much recursion""- Wine-emulated IE6: "- died when trying to call function 500, error was "Not enough memory.""
I also got ~2500 for Opera 9.24 and <5000 for Opera 9.5…Didn't test other browsers.Comment: This doesn't test recursion, though. This just tests how many different functions can be nested. A situation where you only use function recursion instead of loops shouldn't have exactly the same constraints as nested individual functions, imho. I'm just very bad at functional recursion so I can't really test it…Also it would theoretically be possible to optimize for functions with identical function body (which you are using a ton of). So… er… maybe you should create them with Function(), give them some different number to crunch and see what happens? ;D
Fx 3b1 on OSX Tiger, 1,83 GHz Duo, 1024 RAM (almost full…):function 5000 running – FINISHED! 5000 function calls took 568ms
_Grey_ : did I say it was testing recursion? I was trying to figure out the limits of chaining different function calls (which may or may not be the same limit the engine has for recursion).Also, the function bodies are not quite identical – they contain different numbers in the string that is passed to the DOM and in the name of the next function called. I don't write JS compilers myself but I'd be quite impressed if anyone could optimise for similar functions with such differences.The various test results seem to show that most browsers have hard-coded call depth limits. The grand exception is Firefox 3, which calculates the limit dynamically. I wonder if it can give JS that much resources and still avoid a rogue script slowing down or freezing your entire system.
@hallvors: Yes, sorry, I was responding to Waldo, I guess. My apologies. You're right of course.
hello
>> I wonder if it can give JS that much resources and still avoid a rogue script slowing down or freezing your entire system.Firefox pops up a dialog to the user when scripts run for too long, asking if the user wants to stop the script or let it continue. But if a script wants to slow down or freeze a browser, there are much easier ways of doing it. In every browser I've ever tried, freezing the browser up indefinitely was very easy (when purposely trying to do it). I would be very impressed if Opera was the first one to put serious effort into overcoming this.
Well, I can tell you Opera has put some serious effort into that problem ;), for example can you put JavaScript into a never-ending loop likewhile(true);and observe how the UI stays fairly responsive. Because of this we have not implemented the "slow script" warning dialog found in Firefox and (I believe) also in IE. I'm not saying that you can't make Opera's UI freeze with JS tricks but I do think you have to work a bit harder..
Opera 9.51.10071function 4998 runningNo changes even after 9.5 final.
Still 4998 in Opera 10.00.4268
Still 4998 in Opera 10.20.1895
Good news with Opera 10.60!
Opera 10.60- died when trying to call function 16382, error was "Maximum recursion depth exceeded"Google Chromium 6.0.423.0 (48614) Ubuntu:- died when trying to call function 13059, error was "undefined"Firefox 3.6.3- died when trying to call function 3000, error was "too much recursion"test it for yourself:http://kyleabaker.com/temp/func20000.html.zip
Safari 4.0.5 (64 bit 10.6.3 Intel)function 5000 running – FINISHED! 5000 function calls took 3674ms
James Long writes:Opera 10.60 on Fedora 13 (x86_64):function 5000 running – FINISHED! 5000 function calls took 170msFirefox 3.6.7 on same machine:- died when trying to call function 3000, error was "too much recursion"Sorry, Firefox fans…Opera wins again.Distressing news for faithful Opera fans, however…Chrome (just downloaded 64-bit rpm for this test):function 5000 running – FINISHED! 5000 function calls took 364msThe only consolation was that Opera performed the test over twice as fast with several tabs open, while Google Chrome was tested while being the only app running with only 1 tab open.I didn't feel like booting into Windows to test IE, but I doubt it would get any farther than Firefox, if even that far.
function 5000 running – FINISHED! 5000 function calls took 63ms10.61 RC1 on Vista with about 70 tabs open. 🙂
Anonymous writes:James Long is a long time Firefox fan in denial. You heard it here folks.
Anonymous writes:Long live James Long! If opera had a god surely james would be worthy enough to control the power that opera harnesses.