Maximum function parameter length and maximum stack depth detection for JavaScript

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.