Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a max number of arguments JavaScript functions can accept?

Tags:

javascript

I know that JavaScript functions can accept "any" number of arguments.

function f(){}; f(1,2,3,4 /*...*/); 

But I'm wondering if there is actually a limit to how many "any" can be?

E.g., let's say I hand a million arguments to f(). Would that work? Or would the interpreter keel over?

I'm guessing the maximum is either (a) implementation-specific or (b) (2^32)-1, since the arguments object is array-like.

I don't see this mentioned in the language specification, but I might not be connecting some dots.

like image 959
GladstoneKeep Avatar asked Mar 30 '14 17:03

GladstoneKeep


People also ask

How many arguments can a function accept?

Except for functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. This number can be zero. The maximum number of arguments (and corresponding parameters) is 253 for a single function.

How many function arguments is the max you can have?

8192/32 = 256. There is no maximum limit to pass parameters or arguments to a user defined function.

Is there a maximum to the number of arguments a function or module can receive?

Therefore, you can have at most 0xFF == 255 keyword arguments or 0xFF == 255 positional arguments.

Does JavaScript functions check for the number of arguments received?

Parameter Rules JavaScript function definitions do not specify data types for parameters. JavaScript functions do not perform type checking on the passed arguments. JavaScript functions do not check the number of arguments received.


2 Answers

Although there is nothing specific limiting the theoretical maximum number of arguments in the spec (as thefortheye's answer points out). There are of course practical limits. These limits are entirely implementation dependent and most likely, will also depend exactly on how you're calling the function.


I created this fiddle as an experiment.

function testArgs() {     console.log(arguments.length); }  var argLen = 0; for (var i = 1; i < 32; i++) {     argLen = (argLen << 1) + 1;     testArgs.apply(null, new Array(argLen)); } 

Here are my results:

  • Chrome 33.0.1750.154 m: The last successful test was 65535 arguments. After that it failed with:

    Uncaught RangeError: Maximum call stack size exceeded

  • Firefox 27.0.1: The last successful test was 262143 arguments. After that it failed with:

    RangeError: arguments array passed to Function.prototype.apply is too large

  • Internet Explorer 11: The last successful test was 131071 arguments. After that it failed with:

    RangeError: SCRIPT28: Out of stack space

  • Opera 12.17: The last successful test was 1048576 arguments. After that it failed with:

    Error: Function.prototype.apply: argArray is too large

Of course, there may be other factors at play here and you may have different results.


And here is an alternate fiddle created using eval. Again, you may get different results.

  • Chrome 33.0.1750.154 m: The last successful test was 32767 arguments. After that it failed with:

    Uncaught SyntaxError: Too many arguments in function call (only 32766 allowed)

    This one is particularly interesting because Chrome itself seems to be confused about how many arguments are actually allowed.

  • Firefox 27.0.1: The last successful test was 32767 arguments. After that it failed with:

    script too large

  • Internet Explorer 11: The last successful test was 32767 arguments. After that it failed with:

    RangeError: SCRIPT7: Out of memory

  • Opera 12.17: The last successful test was 4194303 arguments. After that it failed with:

    Out of memory; script terminated.

like image 58
p.s.w.g Avatar answered Sep 22 '22 18:09

p.s.w.g


ECMAScript 5.1, part 8.8

The List type is used to explain the evaluation of argument lists (see 11.2.4) in new expressions, in function calls, and in other algorithms where a simple list of values is needed....These sequences may be of any length.

So there is no limit in the standard. Of course, if your code runs in the real world, not in standards-land, there is obviously some limit (e.g. number of particles in the universe).

There are two ways to pass parameters to a function.

  • "Literally": f(a, b, c)
  • With apply(): f.apply(null, [a, b, c])

This latter way is the more realistic scenario for large argument lists.

Go to this JSFiddle to see the limits for each of these for your current browser.

I tried out a few browsers myself:

           | apply() | literal  ---------------------------- Chrome 14  | 131155  |  32767 Chrome 35  | 126213  |  65535 Firefox 30 | 500001  |  65535 IE 9       | 254335  |  65533 IE 10      | 253667  |  65533 IE 11      | 252447  |  65533 Opera 22   | 126063  |  65535 Safari 4   | 524215  | 524205 Safari 7   |  65537  | 522159 

I saw differences in many of these numbers on different machines, so I believe there are other factors at play besides just the browser (the OS?).


This is a real issue, not some trivia. A real-world example:

The Google Closure Library defined the following function.

goog.crypt.byteArrayToString = function(array) {   return String.fromCharCode.apply(null, array); }; 

This worked only if the length of the string was within the browser limit for the number of arguments that can be passed with Function.prototype.apply.

The function was later patched, making the function significantly more complicated.


FYI, there is an open Webkit issue filed in March 2012 that discusses the argument limit.

like image 28
Paul Draper Avatar answered Sep 19 '22 18:09

Paul Draper