performance - For Javascript game engines, is re-using a global variable more or less efficient than making locals? -
a lot of javascript performance guides tend emphasize 2 points:
- stay within scope; looking variables progressively through each scope expensive.
- don't abuse garbage collector creating unnecessary variables constantly.
for programs run @ 60fps or similar high speeds, there difference in performance? jsperf seems go between 2 on system, i'd know little more how optimize type of stuff. considering following 2 code samples:
var t0; function dosomethingglobal() { t0 = getwhatever(); t0.func1(); t0.func2(); }
verses
function dosomethinglocal() { var t0 = getwhatever(); t0.func1(); t0.func2(); }
i think depends on how access globals, , how nested globals in execution context(s). quicker access variable 1 execution context 'higher', 1 ten levels higher, instance (depending on js engine, imagine results , engine optimisations vary).
i've modified test bit have 50 accesses of global variable, , results significant, ~5 times faster local access in particular test (for brief tests in firefox 19.0 , chrome 26).
as performance related issues, rules of thumb can far. you have benchmark your code , make optimisations make sense your code.
Comments
Post a Comment