Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing the arguments object is expensive.. huh?

I've heard alot of people saying that accessing the arguments object is expensive. (example: Why was the arguments.callee.caller property deprecated in JavaScript?)

Btw what exactly does that statement mean at all? isn't accessing the arguments object simply a simple property lookup? what exactly is the big deal?

like image 227
Pacerier Avatar asked May 27 '11 02:05

Pacerier


People also ask

How do you access the arguments of an object in a function?

The arguments object is a local variable available within all non-arrow functions. You can refer to a function's arguments inside that function by using its arguments object. It has entries for each argument the function was called with, with the first entry's index at 0 .

Which keyword is used to access the array of arguments of a function?

You can access specific arguments by calling their index. var add = function (num1, num2) { // returns the value of `num1` console.

What could be the purpose of the arguments inside a function?

In mathematics, an argument of a function is a value provided to obtain the function's result. It is also called an independent variable.

What is arguments in JavaScript?

The arguments is an object which is local to a function. You can think of it as a local variable that is available with all functions by default except arrow functions in JavaScript. This object (arguments) is used to access the parameter passed to a function. It is only available within a function.


1 Answers

The big deal is at least twofold:

1) Accessing the arguments object has to create an arguments object. In particular, modern JS engines don't actually create a new object for the arguments every time you call a function. They pass the arguments on the stack, or even in machine registers. As soon as you touch arguments, though, they have to create an actual object. This is not necessarily cheap.

2) Once you touch the arguments object, various optimizations that JS engines can otherwise perform (e.g. detecting cases in which you never assign to an argument and optimizing that common case) go out the window. Every access to the function arguments, not just ones through arguments becomes much slower because the engine has to deal with the fact that you might have messed with the arguments via arguments.

like image 157
Boris Zbarsky Avatar answered Oct 21 '22 11:10

Boris Zbarsky