GUIDES Optimizing Embedded JavaScript Accessing Properties
About
Binary Data
Index
Convert String to ArrayBuffer
Convert ArrayBuffer to String
Convert ArrayBuffers to String
Handle Errors Converting ArrayBuffer to String
Immutable ArrayBuffers
Resize an ArrayBuffer
Combine ArrayBuffers
Convert Base64 to Binary Data
Convert Binary Data to Base64
Convert Binary Data to Hex
Convert Hex to Binary Data
Calculate CRC for Binary Data
Compress Binary Data – One Buffer
Compress Binary Data – Streaming
Decompress Binary Data – One Buffer
Decompress Binary Data – Streaming
Callbacks
Index
One-Time Callback
Repeating Callback
Repeating Callback with Initial Delay
Immediate Callback
Reschedule Callback
Cancel Callback
Suspend Callback
What About setTimeout?
Optimizing Embedded JavaScript
Index
When to Optimize
Know Where to Optimize
Looping through an Array
Iterating Over a String
Building a String
Avoid Copying Buffers
Accessing Properties
Map versus Object
Appending to an Array
Operating on Bits
Defining Class Methods
Reducing Stack Use
Time
Index
Get Unix Time
Get Time of Day
Get Date
Get Time Since System Start
Get Microseconds
Set System Date and Time
Set Real-Time Clock Time
Get Time and Date from Network
Get Time and Date from Real-Time Clock
Sleep

Accessing Properties

The most direct way to access a property is by name: object.foo. An alternative is to write object["foo"]. These are equivalent, but the form object.foo is more efficient. That's because when you use the object["foo"] the JavaScript may need to convert the string to an internal XS identifier at runtime.

If the value between braces ([]) is a constant that is a valid JavaScript property name, use the direct form.

The most common use of the object["foo"] form is in with a for-in loop. In this case the code using the [] notation doesn't know the name of the property, so it cannot use the more direct object.foo form. Another reason is because a property name cannot be expressed using the direct form, for example because it is a number or contains a space.

for (let name in obj) {
	const value = obj[name];
	trace(`${name} = ${value}\n`);
}

let v1 = obj[12];
let v2 = obj["a property"];

Note: Often when code uses [] notation to access properties, it is a sign that a Map may be a better choice.