Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do unused paramenters in functions slow down the execution of JavaScript?

Does having unused parameters in javascript functions slow down execution? Does it take up memory? I often write functions with parameters that are never actually used for example function has an event as a parameter, but the event is never used for anything.

like image 246
Firze Avatar asked Jan 08 '15 07:01

Firze


People also ask

Do unused variables affect performance JavaScript?

Unused local variables make code hard to read and understand. Any computation used to initialize an unused variable is wasted, which may lead to performance problems.

What happens if you don't pass any parameter in the JavaScript function?

Nothing will happen- meaning you won't get an error or a warning as passing the parameters in javascript is optional.

Do JavaScript functions need parameters?

There is no need to specify the data type for parameters in JavaScript function definitions. It does not perform type checking based on passed in JavaScript function.

How many parameters should a function have in JavaScript?

When you call a function in JavaScript, you can pass in any number of arguments, regardless of what the function declaration specifies. There is no function parameter limit. In the above function, if we pass any number of arguments, the result is always the same because it will take the first two parameters only.


2 Answers

slow down execution?

The running time depends upon the operation being performed on the input. May it be a search or a sort or any simple operation. The running time of the operation determines the speed of its execution. So when you don't pass any parameter, that means no action is being performed on that parameter and hence the running time is stable.

Does it take up memory?

But as far as memory allocation is concerned, they are generally allocated a Lexical Environment.

When the function runs, on every function call, the new LexicalEnvironment is created and populated with arguments, variables and nested function declarations.

So for the below function:

function sayHi(name) {
/* LexicalEnvironment = { name: passedvalue, 
                        phrase: undefined }*/  // created at this point.
  var phrase = "Hi, " + name
  alert(phrase)
}

So when you invoke it as sayHi(), the lexical envirmonemt looks like:

LexicalEnvironment = { name: undefined, phrase: undefined};

Hence, each parameters and function variables are allocated memory when the interpreter interprets the function.

When the interpreter is preparing to start function code execution, before the first line is run, an empty LexicalEnvironment is created and populated with arguments, local variables and nested functions.

like image 90
BatScream Avatar answered Nov 12 '22 10:11

BatScream


JavaScript anyways stores parameters sent to every function in the arguments array, irrespective of you allocating local variables for them.

function test() {
    console.log(arguments)
}

test('these','are','passed')

//logs ["these','are',passed']

My point is that the unused parameters are anyways taking up memory, you are only referencing them using another variable name.

like image 22
surajck Avatar answered Nov 12 '22 11:11

surajck