html5 - Javascript Performance Gains from Object Literals vs Functions -


so, let's think big here. let's have entire mmo want put together. naturally, involves lot of resources mobs, items, spritesheets, tilemaps, dialogue, characters, etc etc. in typical game engine, you'd load or unload these resources based on arbitrary designator, such map or "zone".

is there javascript version of this? example, able to, while game loop running.

(pseudocode)

if playerposition = therightplace {     load resources next zone     unload resources previous zone } 

is there performance gain in doing this? or should load resources game , done it? in zones have class uglymonster , in zone have ugliermonster.

i can instantiate these 2 classes either via john resig's classical inheritance class structure (which i've been using until hit conversation starter, , handy kind of program)

var uglymonster = monster.extend({     stuff why ugly;     make position!     attack player!!! });  var monster1 = new uglymonster(); 

or function

function uglymonster(name, position) {     create ugly monster!!!! attack player!     stuff why ugly }  var monster1 = new uglymonster('fred', 100); 

if declare them variables, monsters in game sitting in global variable, not? there way declare variables without constructing them? i'm new whole inheritance game in javascript, perhaps i'm missing obvious.

what's best way handle these kinds of classes floating around? leave them? remove them? instantiate via functions? make noticeable difference?

i hope i've stated enough hope accomplish. thank you!

i'm not totally sure you're asking,

function uglymonster(name, position) {    ... } 

and

var uglymonster = function (name, position) {     ... } 

are equivalent expressions. functions objects in javascript. in both of these cases these functions saved variables , attached properties on global object.

there won't noticeable performance difference in these 2 approaches.

as far

is there way declare variables without constructing them?

you can declare variable without defining it. var x;. or can declare class constructor without instantiating object of class immediately. you'll have defined function won't have been run yet.

update

a bit more background on how functions read in javascript.

when javascript engine interpreting new scope, starts hoisting variable declarations in code top of current scope (the global scope or current function). variables "exist" undefined. processes function declarations, they're moved top default.

so following code

var x = 3+2;  var uglymonster = function (name, position) {     ... }  function uglymonster2(name, position) {    ... } 

is interpreted if written this:

var uglymonster, uglymonster2,x;  uglymonster2 = function(name, position) {    ... }  x = 3+2;  uglymonster = function (name, position) {     ... } 

its important note no functions have been run in example. they've been defined. no functions run , no instances of uglymonster created until run

new uglymonster(). if creating many monsters there may value in holding off on creating these long possible. in end though, performance going depend on many things , surefire way tell test , try things out yourself.


Comments