performance - JavaScript object property assignment -
on page -> https://developer.mozilla.org/en-us/docs/web/javascript/a_re-introduction_to_javascript
" once created, object's properties can again accessed in 1 of 2 ways:
obj.name = "simon"; var name = obj.name; obj["name"] = "simon"; var name = obj["name"];
these semantically equivalent. the second method has advantage name of property provided string, means can calculated @ run-time though using method prevents javascript engine , minifier optimizations being applied."
(in bold) why second method better? javascript , minifier optimizations referred here?
please let me know if need more information
the second not 'better' safer, allows properties unconventional characters. example:
obj.my-property = true; //parse errror obj['my-property'] = true; //fine, because property name specified string
it allows build dynamic property names, e.g.
var prop_name = 'foo'; prop_name += 'bar'; obj[prop_name] = true;
the optimisation/minification point has fact if not minifiers leave strings strictly untouched, reasons obvious. had:
var str = "hello";
even if minified script, want string stay "hello".
so minification engine may turn
obj.foobar = true;
into
a.b=true;
but wouldn't change
obj['foobar'] = true;
into
a['b']=true;
Comments
Post a Comment