1

If I have a simple array constant that I want to use, what is better in terms of performance when using local vs global declaration?

// myScript.js
const usefulArray = ["say", "about", "10", "array", "elements"];

export const someFunction = (anotherArray) => {
   // const usefulArray = ["say", "about", "10", "array", "elements"];
   return anotherArray.concat(usefulArray);
}

I know in terms of memory, local will be on a stack and popped out after function returns. And global will remain in memory for the life of the process.

Is there a performance benefit to having usefulArray declared globally in this function? When should I use global and local?

2 Answers 2

3

It shouldn't matter in 99% of cases, especially if there are only 10 array elements.

The one situation in which it might matter would be if your someFunction was run extremely frequently, like tens of thousands of times. In such a case, every time it runs, it would have to create a new usefulArray, and then add its elements onto the passed anotherArray. The overhead of creating a new initial array in addition to the returned, concatenated result might not be zero. It would also depend on the Javascript engine - some may be able to optimize it better than others.

In contrast, if you create the array outside the function, since it only has to be created once, that could result in fewer operations required over many many calls of someFunction.

But this shouldn't be a concern. It's extremely unlikely that the code here is being run in such a tight loop that performance is an issue. Better to strive for code clarity, and optimize later only if necessary. Premature optimization is a bad idea.

In terms of code clarity, it's probably a good idea to reduce a variable's scope as much as possible - if there's anything else on the top level of that module, it might not be the best idea to have usefulArray also defined on the top level, when you only intend it to be used in someFunction. You might consider defining it such that it's only scoped to someFunction, rather than to anything else in that module.

Keep in mind that you can also avoid the variable entirely, if you wish:

export const someFunction = anotherArray => anotherArray.concat(
  ["say", "about", "10", "array", "elements"]
);
Sign up to request clarification or add additional context in comments.

Comments

0

If you call this functions A LOT, then you could have some benefit to have usefulArray global, since if you have it local it means you're creating the same array over and over (for each function's call) and there is no need.

The problem is, if it's really global it could be lead to dangerous side effect: if usefulArray is global, some other functions might change it (const means you cannot re-assign the variable, but since it points to an array the content CAN change), so two calls to someFunction with the same arguments might lead to different results. This of course won't happen if you create the array locally.

However it seems from your code that usefulArray is not global, since you're using modules. So it's global to your module, but not to your "application" – it will persist, but it can't be manipulated outside the module where it's declared.

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.