The general code may not involve the maximum length of the reference and the maximum stack depth, but for some special occasions, it is necessary to detect these two parameters. For example: using recursion to calculate the nth value of the Fibonacci sequence, without knowing the maximum stack depth, is inevitably superficial. Another example: turning a string of charcode into a string without knowing the maximum length of the reference. The use of string splicing, the efficiency cannot be raised. Especially in the case of long strings.
The following two methods implement the maximum function length detection and maximum stack depth detection of JavaScript execution environment respectively.
Different browsers have different test results, the same browser in different machine test results are different. Even some browsers have different results when they test continuously.
function Getmaximumsupportedargumentslength () {var args={length:0};function noop () {}function test (n) {args.length=n; Try{noop.apply (Null,args);} catch (e) {return false;} return true;} function Getnum (upTo) {if (Test (UpTo)) {return upTo;} var Min=1,max=upto,mid=math.floor ((Min+max) *0.5), while (Min<max) {if (Test (mid)) {Min=mid;} Else{max=mid;} Mid=math.floor ((Min+max) *0.5); if (mid==min) {break;}} return min;} Return Getnum (0xFFFFFFFF);} function Getmaximumcallstacksize () {var size=0;function test () {size++;test ();} Try{test ();} catch (e) {return size;}} Maximum supported Arguments length//ch34 124782//ff29 500000//sf7 65536//maximum call Stack size//ch34 20926 in < ; script>, 20804 in console//ff29 21### (varies) in <script>, 49993 in Console//sf7 43517 in <script>, 43 509 in console
Maximum function parameter length and maximum stack depth detection for JavaScript