I have a quite complicated question to ask :)
I am currently working on a html5 canvas game. The variables which are specific to a map of the game are in a separate file (let's call it game.js), separated from the game engine (let's call it engine.js).
I have read that global variables are slower to use in JS than local variables. Therefore, in game.js, I create a global variable which contains all the game-specific variables. In engine.js, I copy this global object to local variables, there I delete this global object.
This is working. But I know that assigning objects only pass a reference to these objects.
Therefore my question is: will the performance of that be as if I had declared all the variables directly as local variables in engine.js, as I delete the global object at the end of the initialisation, or will it be slower, as if my local variables in engine.js were just references to a global object?
I could declare all the variables as local in engine.js, but it would be useful for me to separate what is specific to the map, if later I want to make other maps/games.
For example:
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, will the performance of x, y and obj be as fast as local variables, or slower?
Note: I didn't really check the difference between performances of global vars, and local vars, but I assume what I read about it is right.
Hope my question was clear enough and not silly :) If you have any ideas... Thanks!