I have a very basic JavaScript question.
I am writing a program which will generate JavaScript code. for accessing a property of a variable i have two choices:
1. make the property access a static query. i.e.
var result = object.property
OR
2. make the property access a dynamic query, i.e.
var result = object["property"]
The difference it makes to me is that for the first case (static query case) i will have to generate separate code for each property access. whereas in the second case (dynamic query case) i can reuse the same function for every property.
I can decide if i know does this makes any difference in performance?
is obj.property
faster or obj["property"]
?
May be this also depends on the engine which will be used to interpret javascript, so i must mention that I will be using Rhino as my javascript engine.
So please throw some light on this issue.
Thanks, Regards, VImal
In terms of performance comparison, var is faster and let is slower inside the loops while running or executing the code. Re-declaring var declared a variable in the same function or scope gives rise to Syntax Error whereas let declared variable cannot be redeclared.
Property accessors provide access to an object's properties by using the dot notation or the bracket notation.
A JavaScript property is a characteristic of an object, often describing attributes associated with a data structure. There are two kinds of properties: Instance properties hold data that are specific to a given object instance. Static properties hold data that are shared among all object instances.
This data within JavaScript is contained as fields (properties or variables) and code (procedures or methods). Properties and variables are similar in nature, but properties are specifically tied to objects while variables are not.
There are no static properties in Javascript, only dynamic property accessing exists.
Properties are always queried in the same way regardless of what syntax you put in your source code file.
Use jshint to recommend good source code conventions for your JS files:
http://jshint.com/
Dot notation is always recommended. Use quotation mark notation only if your Javascript property has not id which passes in JS syntax.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With