Search code examples
javascriptparametersecmascript-6standardsecmascript-5

How many parameters are too many in JavaScript?


I came across the following question on StackOverflow: How many parameters are too many?

This got me thinking, is there a practical limit imposed on the number of parameters of a JS function?

test(65536); // okay
test(65537); // too many

function test(n) {
    try {
        new Function(args(n), "return 42");
        alert(n + " parameters are okay.");
    } catch (e) {
        console.log(e);
        alert(n + " parameters are too many.");
    }
}

function args(n) {
    var result = new Array(n);
    for (var i = 0; i < n; i++)
        result[i] = "x" + i;
    return result.join(",");
}

Turns out, JavaScript imposes a practical limit of 65536 parameters on functions.

However, what's interesting is that the error message says that the limit is 65535 parameters:

SyntaxError: Too many parameters in function definition (only 65535 allowed)

So, I have two questions:

  1. Why this discrepancy? Is it an off-by-one error in the language implementations?
  2. Does the ECMAScript standard impose this limit on function parameters?

Solution

  • Argument length limited to 65536 https://bugs.webkit.org/show_bug.cgi?id=80797

    There are different argument count limits, depending on how you test: http://mathiasbynens.be/demo/javascript-argument-count