After stumbling upon a question on StackOverflow discussing the number of parameters in JavaScript functions (How many parameters are too many?), I started pondering if there is a real limitation on how many parameters a JS function can have.
test(65536); // everything's fine here
test(65537); // uh-oh, that's too many
function test(n) {
try {
new Function(args(n), "return 42");
alert(n + " parameters are acceptable.");
} catch (e) {
console.log(e);
alert(n + " parameters are too much.");
}
}
function args(n) {
var result = new Array(n);
for (var i = 0; i < n; i++)
result[i] = "x" + i;
return result.join(",");
}
Evidently, JavaScript sets a practical limit of 65536 parameters for functions.
What adds to the intrigue is that the error message claims the maximum to be 65535 parameters:
SyntaxError: Too many parameters in function definition (only 65535 allowed)
So, I'm left with two questions:
- Why does this inconsistency exist? Could it be an off-by-one error in language implementations?
- Does the ECMAScript standard actually enforce this limit on function parameters?