6

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?
Community
  • 1
  • 1
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
  • 2
    It depends on the VM running javascript http://stackoverflow.com/questions/22747068/is-there-a-max-number-of-arguments-javascript-functions-can-accept – Noctisdark Mar 11 '16 at 18:55
  • @Noctisdark Turns out in Firefox the 65535 limit on function parameters is respected. I can't test IE because I'm using Linux. Since Opera is now just Google Chrome under the hood I'm assuming that it behaves the same way Chrome does. – Aadit M Shah Mar 11 '16 at 19:02
  • Not that it makes a big difference here, but the test is flawed since it is also constrained by the maximum number of elements an array supports. – Felix Kling Mar 11 '16 at 19:14
  • Hmmm, I am not sure if the ECMAscrupt imposes this kind "limit, why would you need a function with 65335 argument", try this ```Array.prototype.slice.apply(null, new Array(65335));```, why is it a syntax error ? – Noctisdark Mar 11 '16 at 19:18
  • @Felix http://stackoverflow.com/questions/6154989/maximum-size-of-an-array-in-javascript – Noctisdark Mar 11 '16 at 19:20
  • @Noctisdark: Not sure what you are trying to tell me. – Felix Kling Mar 11 '16 at 19:20
  • @FelixKling Probably that 4.29 billion elements is quite a lot. – Aadit M Shah Mar 11 '16 at 19:21
  • Max size of an array is a lot larger than 65335, I think that should not affect the test – Noctisdark Mar 11 '16 at 19:21
  • @Noctisdark: That's why I said *"Not that it makes a big difference here"*. Maybe I should have added *"in theory"* somewhere to my comment. Point is that this test is testing two things, not one. – Felix Kling Mar 11 '16 at 19:22
  • Aw, sorry T_T allways skip some parts while reading stuff, the interesting thing here is, why is this a syntax error ? – Noctisdark Mar 11 '16 at 19:25

1 Answers1

5

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

ObjectMatrix
  • 325
  • 1
  • 3
  • 9