Is There a Max Number of Arguments JavaScript Functions Can Accept

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

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.

How many parameters are too many in JavaScript?

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

Max Argument/Parameter in a JS function

ECMAScript 5.1

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.

Theoretically there is not a maximun, but your memory is not infinite. so in practice, is limited.

You can read some experiments in this answer

Unlimited arguments in a JavaScript function

There's a weird "magic" variable you can reference called "arguments":

function manyArgs() {
for (var i = 0; i < arguments.length; ++i)
alert(arguments[i]);
}

It's like an array, but it's not an array. In fact it's so weird that you really shouldn't use it much at all. A common practice is to get the values of it into a real array:

function foo() {
var args = Array.prototype.slice.call(arguments, 0);
// ...

In that example, "args" would be a normal array, without any of the weirdness. There are all sorts of nasty problems with "arguments", and in ECMAScript 5 its functionality will be curtailed.

edit — though using the .slice() function sure is convenient, it turns out that passing the arguments object out of a function causes headaches for optimization, so much so that functions that do it may not get optimized at all. The simple, straightforward way to turn arguments into an array is therefore

function foo() {
var args = [];
for (var i = 0; i < arguments.length; ++i) args[i] = arguments[i];
// ...
}

More about arguments and optimization.

Is it possible to send a variable number of arguments to a JavaScript function?

Update: Since ES6, you can simply use the spread syntax when calling the function:

func(...arr);

Since ES6 also if you expect to treat your arguments as an array, you can also use the spread syntax in the parameter list, for example:

function func(...args) {
args.forEach(arg => console.log(arg))
}

const values = ['a', 'b', 'c']
func(...values)
func(1, 2, 3)

And you can combine it with normal parameters, for example if you want to receive the first two arguments separately and the rest as an array:

function func(first, second, ...theRest) {
//...
}

And maybe is useful to you, that you can know how many arguments a function expects:

var test = function (one, two, three) {}; 
test.length == 3;

But anyway you can pass an arbitrary number of arguments...

The spread syntax is shorter and "sweeter" than apply and if you don't need to set the this value in the function call, this is the way to go.

Here is an apply example, which was the former way to do it:

var arr = ['a','b','c'];

function func() {
console.log(this); // 'test'
console.log(arguments.length); // 3

for(var i = 0; i < arguments.length; i++) {
console.log(arguments[i]);
}

};

func.apply('test', arr);

Nowadays I only recommend using apply only if you need to pass an arbitrary number of arguments from an array and set the this value. apply takes is the this value as the first arguments, which will be used on the function invocation, if we use null in non-strict code, the this keyword will refer to the Global object (window) inside func, in strict mode, when explicitly using 'use strict' or in ES modules, null will be used.

Also note that the arguments object is not really an Array, you can convert it by:

var argsArray = Array.prototype.slice.call(arguments);

And in ES6:

const argsArray = [...arguments] // or Array.from(arguments)

But you rarely use the arguments object directly nowadays thanks to the spread syntax.



Related Topics



Leave a reply



Submit