I have a pretty tricky question :)
I am currently working on a html5 game. The variables that relate to the game map are in a separate file (let it be called game.js), separated from the game engine (call it engine.js).
I read that global variables are slower to use in JS than local variables. So in game.js, I create a global variable containing all the variables related to the game. In engine.js, I copy this global object to local variables, there I delete this global object.
It works. But I know that assigning objects only passes a reference to these objects.
So my question is: will the performance be as if I would declare all the variables directly as local variables in engine.js since I would delete the global object at the end of initialization or would it be slower as if my local variables in engine.js were just references to a global object?
I can declare all variables as local in engine.js, but it would be useful for me to separate what is typical for a map if I want to make other maps / games later.
For instance:
game.js:
Game = function() { this.x = 16; this.y = 16; this.myObject = {"test": "hello", "action": "none"}; } game = new Game();
engine.js: // ...
var x = game.x; var y = game.y; var obj = {"test": game.myObject.test, "action": game.myObject.action};
// ...
In this example, the performance of x, y and obj will be as fast as local variables, or slower?
Note. I really did not check the difference between the characteristics of global var and local var, but I assume that I read about it correctly.
I hope my question was clear enough, not stupid :) If you have any ideas ... Thank you!