Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript property access speed: difference var.property vs. var["property"]?

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

like image 512
weima Avatar asked Jul 20 '12 13:07

weima


People also ask

Which is faster let or VAR?

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.

What is property access in JavaScript?

Property accessors provide access to an object's properties by using the dot notation or the bracket notation.

What are the properties of JavaScript?

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.

Are properties variables in JavaScript?

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.


1 Answers

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.

like image 71
Mikko Ohtamaa Avatar answered Oct 26 '22 19:10

Mikko Ohtamaa