102

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.

8
  • 3
    Also, ANY large sequence of data like this should be passed as a single argument that's an array, not as separate arguments so an entire copy need not be made of the large data set just to call the function. This has to be an academic exercise, not something that would be encountered with good development practices.
    – jfriend00
    Mar 30, 2014 at 17:47
  • 1
    @jfriend00, don't forget about Function.prototype.apply. stackoverflow.com/questions/1379553/… Jul 6, 2014 at 10:54
  • @PaulDraper - I know about .apply(), but it's making a copy of the input array too. The point is that any interface that expects to accept a very large number of arguments shouldn't be declared to require them to be passed as normal arguments. Much more efficient to pass an array so no copy of the data need be made.
    – jfriend00
    Jul 6, 2014 at 18:08
  • 7
    @IngoBürk This is not a theoretical question! Even if you never explicitly write such a function, if you use the ES 2015 spread operator and functions like Math.max or Array.prototype.concat(...arrayOfArrays) (to flatten an array of arrays) you can get to that limit dynamically, without having written a monster function. The overhead mentioned by jfriend00 does not always matter - clarity of code is more important than a few bytes of memory or CPU cycles, and the spread syntax may sometimes be the best choice in that regard. Look at alternatives for "flatten array" to my short code...
    – Mörre
    Sep 13, 2017 at 10:04
  • @Mörre My comment is from March 2014. ES5015 wasnt released until June 2015.
    – Ingo Bürk
    Sep 13, 2017 at 10:54

8 Answers 8

123

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 262,143 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 131,071 arguments. After that it failed with:

    RangeError: SCRIPT28: Out of stack space

  • Opera 12.17: The last successful test was 1,048,576 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 32,767 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 32,767 arguments. After that it failed with:

    script too large

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

    RangeError: SCRIPT7: Out of memory

  • Opera 12.17: The last successful test was 4,194,303 arguments. After that it failed with:

    Out of memory; script terminated.

5
  • This is great. Although if you're using apply and sending arguments as an Array, the theoretical max per the spec would be 4,294,967,295 elements no matter what the practical limits are (?). Mar 30, 2014 at 17:50
  • 2
    Your example is testing the max of .apply() more than function arguments passed as shown in the question. They're not necessarily the same. Here's a demo where I've created a function from a string using the Function constructor. The function invokes your testArgs() function, passing it 100,000 arguments. Merely creating that function fails with SyntaxError: too many function arguments Mar 30, 2014 at 17:58
  • @cookiemonster You're correct. There are many different factors that may limit the maximum number, and different methods for calling it may yield different results. I suppose I should emphasize the first sentence a bit more.
    – p.s.w.g
    Mar 30, 2014 at 18:03
  • Yeah, it all depends on what the OP has in mind. I can't imagine typing out 100,000 arguments to a function, so .apply() would seem more likely. But then he didn't mention .apply(), so maybe he's planning on doing some evaling. Mar 30, 2014 at 18:04
  • @GladstoneKeep Correct. My tests are about finding out the approximate practical limits in a given browser.
    – p.s.w.g
    Mar 30, 2014 at 18:31
25

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
Chrome 106  | 125625  |  62803
Firefox 30  | 500001  |  65535
Firefox 106 | 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.

7

According to ECMA Script 5.1 Standard Specification for List,

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. Values of the List type are simply ordered sequences of values. These sequences may be of any length.

So, the standard doesn't have any limitations on the number of arguments and limited only by the memory.

4

Computer Stupidities
> from Computer Stupidities

As far as I'm concerned, the limit is "big enough"!

2
  • 8
    "the limit is 'big enough'!" Unless it isn't. bugs.webkit.org/show_bug.cgi?id=80797 Jul 7, 2014 at 6:25
  • 1
    This answer distracts from p.s.w.g's answer below. "Big enough" is not helpful when you are providing arguments dynamically, for example String.fromCharCode(...big_array_of_char_codes). Aug 16, 2022 at 6:05
1

It's purely dependent on how much powerful the client is.

Because the browser will lose its memory, if you are gonna pass millions of arguments.

Each variable holds some memory. So browsers will have no problem allocating each variable some memory, until it itself has no memory to run on.

1

Here is how I check it.

let args = [];

while(true){
    try{
        (() => {})(...args);
        args.push(0);
    }catch(e){
        console.log(e.message);
        console.log(`Number of arguments: ${args.length}`);
        break;
    }
}
1

Just use an map (just an object) as a single parameter. You still get to use parameter names and can have as many arguments as you'd like. It guarantees you a theoretical infinite number of arguments you can pass in, however you obviously still remain bounded by heap space.

let infiniteParameters = (w) => console.table(w);

infiniteParameters({
  name1 : "value1",
  name2 : "value2",
  // ...
});
2
  • This does not answer the question. The question was "Is there a maximum number of arguments JavaScript functions can accept?" Not "How can I pass a theoretically infinite number of arguments to a JavaScript function?"
    – Matthew W.
    Sep 26, 2021 at 6:14
  • @MatthewW. It's still a useful answer as it's a likely reason for the question being asked. It also helps others who may be searching for this type of solution. Mar 30, 2022 at 8:39
-1

According to MDN's JavaScript Function section, the maximum amount of parameters a function can accept is 255.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions#Defining_functions

param The name of an argument to be passed to the function. A function can have up to 255 arguments.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.