0

I have taken some JavaScript courses lately (at codeschool) and, in one of them, they said:

  • A function declaration (fd) builds in memory immediately when program loads;
  • A function expression (fe) builds in memory dynamically during execution.

Thus fe has an advantage over fd which is saving some memory at loading time - I concluded.

So, for example, considering following two snipped codes below, there won't be any extra memory allocation on loading time because foo is going to be assigned to undefined thanks to hoisting.

// Original code
var foo = function (bar) {
  console.log(bar);
};

Below we have code after hoisting

// Code after hoisting
var foo = undefined;

foo = function (bar) {
  console.log(bar);
};

In shortness, foo is undefined and the function expression will only be assigned to foo once code runs. Thus it won't allocate extra memory on loading time.

Ok. No problems till now. However, "if fe has an advantage over fd which is saving some memory at loading time" following code should take some extra memory to be allocated at loading time since it's a function declaration.

function foo(bar) {
  console.log(bar);
}

So the question is: is my reasoning right? If so, how much memory does it take at loading time and how is it calculated?

1 Answer 1

3

Yes, you're right because all function declarations are hoisted to the top and (usually) compiled on the spot. Function expressions aren't generally compiled until run-time because they may or may not be used at all:

var f;
if (condition) {
  f = function() { ... };
}
// f could be undefined

As for how much memory it takes, that depends on the engine implementation. How they store:

  1. The executable code for the function
  2. Bindings for argument names
  3. Closure values
  4. String value of the code (if the engine supports it)
  5. Any number of other values that the engine developers have decided they require to implement functions

all depends on the internal design of the engine. You could perform some testing by performing a heap dump before and after functions are created but that actual number would vary as optimizations are added and dropped.

Sign up to request clarification or add additional context in comments.

6 Comments

Answer this " If so, how much memory does it take at loading time and how is it calculated?" lol
@AdamBuchananSmith Like I said, it depends on the engine. The "calculation" is done based on the number and size of the data fields to represent a function in the engine. I also mentioned you could do your own calculations by taking a heap snapshot before and after declaring a function.
Well I didn't even read your answer all the way through, I thought there was no chance for you to answer it.... There still isn't lol
@AdamBuchananSmith I don't understand. I've answered the question as best as it can be answered. "It depends" isn't a very fulfilling answer but it's the only honest answer anyone can give without doing extensive testing across every single possible browser then perhaps coming up with some median size or something.
I am going to up-vote you. I merely said it for a little sarcasm. Because there is no real answer for it. Your answer does answer the rest of his question well. though.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.