memory - JavaScript- properties and variables -
in code deleting custom-objects. realized, can not delete objects declared using 'var'. (after reading documentation delete operator).
- most of objects declared , initialized using 'var'. can't deleted in single delete statement(unless loop through , delete each property.) can set them 'null'.
- my custom objects relatively bigger in size (may block considerable chunk of memory.
for better memory management , performance should use 'properties' or 'variables' store objects? make difference?
please provide references better insight on 'properties' , 'variables' in javascript.
globalprop = {//can deleted, 'property' global object. 'name': 'mia', 'age': 34, 'sex': 'f' }; var globalvar = {//can not deleted delete statement, variable of global object 'name': 'lia', 'age': 25, 'sex': 'f' };
(in above code, can refer globalprop 'window.globalprop' can't refer globalvar window.globalvar.)
javascript has runtime , garbage collector complex cleaning , managing algorithms, try interfere or speed them proably opposite, resulting runtime penalty. best leave memory managment underlying runtime.
however, there points might want consider:
1) avoid global variables as possible. advice language support global/static variables. global variables means global references no scope - referenced object live on long (global) reference holds it. danger increases more if global variable array or object keeps on appending children itself. common case leads memory leak in managed languages
2) avoid cyclic references - don't make 1 member variable point different object, , object holds member points first object. make hard time garbage collector.
3) write clean, simple, maintainable code. may seems unrelated preach, easier, short, maintainable code enhance interperter/compiler ability optimize code.
4) don't assume nothing how code affects speed. there many layers down there can't assume anything. benchmark code real facts.
5) don't optimize unless need (you don't).
so question : declare var
as possible. declaring variable var
make them local - when function ends reference out of scope , gc can start determine destruction time object earlier. declaring object without var
make them global - unclear when gc decide delete object, slowing things down. , know - declaring variables global can lead many bugs, keep things scoped. don't use delete
. it's wrong.
Comments
Post a Comment