0
async function getDataFromExternalAPI(bodyPayload) {
  // send request and get data.
}


var bodyPayload = {a: 1, b: {}};

await getDataFromExternalAPI(bodyPayload);

vs

await getDataFromExternalAPI({a: 1, b: {}});

What are memory implication of using the first method? First method is cleaner in terms of redablity and maintainibility but does it comes with downside ?

3
  • You will not be able to use a variable used foo in the first code's scope. It might cause confusion, if you are using the same foo again and the function is not pure. Changing object value inside the function will reflect outside too Commented Sep 24, 2021 at 5:36
  • Neither of your test() functions make sense here. You're using await as if you're calling a function named test() and passing it an argument, but then you're attempting to define a function body as if this is supposed to be a function declaration. Something is not clear here and probably syntactically wrong. Please clarify your question with proper code examples. Commented Sep 24, 2021 at 5:41
  • @TusharShahi what if I make is const ? I am basically more concern with the memory aspect of it. PS- update the example with more redable code. Commented Sep 24, 2021 at 6:34

1 Answer 1

1

What are memory implication of using the first method?

Nothing worth using that to decide which way to write it (see details below)

First method is cleaner in terms of readability and maintainability but does it comes with downside ?

No downside. Choose whichever option makes the code the cleanest to read and maintain.

What if I make is const?

Making it const won't really change its memory use here. const is always preferred when you don't need to assign to the symbol again because that's a sometimes useful hint to the interpreter and optimizer, but it does not change the memory use in this example.

FYI, var is pretty much never the preferred choice any more. Use let or const.


Objects are passed as argument as pointers. No copy of an object is made when it is passed as an argument.

Both of your code examples are just constructing an object and then passing it as an argument. The first is giving it a named symbol so that other code within this top level scope in your example could access it and its lifetime may be longer because other code in this scope could access it before it is garbage collected.

The second example is also just constructing an object and then passing it as an argument, but because it isn't a named symbol in this scope, the only place that object can be used is within the function (unless it's returned from the function somehow or passed into other lasting scopes from within the function).

Which to use is really just a matter of preferred programming style and which makes any given circumstance more readable and maintainable. In general, I would prefer the second option if the object is only used as the argument and if the object construction doesn't require many separate steps and if no other code uses the same object and if the definition is simple.

Now if the building of this object was a complicated endeavor or there was real code clarity value to applying a meaningful name to the argument object, I'd optimize the code for how clearly I could write that complicated endeavor and that would probably involve defining a symbol in the top scope and then setting properties on that object. So, I'm optimizing for whatever makes the clearest to read code because there are not meaningful memory implications between the two.

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

Comments

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.