I am currently working on a virtual environment using Three.js, where I am incorporating 3D elements and spatial sound. This kind of project can be quite demanding in terms of performance. I have a function that is called every frame (approximately every 1/60th of a second), and I am trying to determine the best placement for constant variables in order to minimize resource usage.
My initial thought was to place the constant variables in the global scope so they do not need to be reassigned with each frame. However, I am aware of the concept of "polluting" the global scope and prefer to avoid it whenever possible. Additionally, these variables are only required within that particular function.
Should I proceed with putting it in the global scope, even though it may slightly clutter it? Is there a difference in performance between re-assigning the variable each time or reading it from the global scope?
Below is the function where the constant variable EASING_FACTOR
is used:
function easeTransition(current, old) {
const EASING_FACTOR = 100;
let result = {};
for (const arg in current) {
const diff = current[arg] - old[arg];
result[arg] = old[arg] + diff / EASING_FACTOR;
}
return result;
}
Thank you!