Object.A meteor falls from above. This is an object from outer space. It has effects—it forms a crater on impact. It has parts (minerals).
Lookup example.Imagine some animals like a cat and a dog. Each has an associated number like a weight. We can place these animals in an object and get this number.
Note: We use a special literal syntax to declare the dictionary object. Keys and values can be added (or removed) later too.
Function.With objects we can place a function in the value part for a key. So when the associated key is used, it can be called like a function.
Here: The box object has a width and height. We specify a function() for the "area" key and call it directly.
Object.keys.Consider an object. It has many keys, but we cannot access them in an easy way like we can an array. We must use a method like Object.keys to get an array of the keys.
Warning: This method returns an array. It also is defined by the specification to loop over the entire object. So it may be slow.
Key count.An object has no length property. But it has a certain number of keys. With Object.keys we can get those keys, and then use length on that variable.
Result: This object has three keys added to it. With Object.keys we discover this information.
Quote: For each own enumerable property of O whose name String is P, call the DefineOwnProperty internal method of array with arguments ToString(index).ECMAScript Language Specification: ecma-international.org
Warning: According to the language specification, this method loops over the object's internals. Be careful with performance here.
In operator.Sometimes we wish to see if a key exists in an object. We can use the "in" keyword to search the keys and return true if a key exists.
Tip: This syntax can be used for feature detection—we can find supported properties on the "window" object, for example.
Nested object.An object can be part of another object. And that nested object can also have sub-objects. We can construct entire trees of objects in this way.
Here: We create an object that has a sub-object. We then link that object to the original object, creating a circular reference.
Warning: This example helps us see how objects may be linked together, but it is not helpful in many real programs.
Multi-map.Sometimes we want to look up an array from a key. This type of collection is sometimes called a multi-map. One key has multiple values.
Program: This program creates an empty object with the identifier "test." It then invokes the add() function to add things to the object.
Add: This function sees if an array exists at the requested key. If no array exists, a new, 1-element array is created.
Push: If an array exists already, we push another value to it. So we add an element to an existing array.
Result: In a browser like Chrome the results are about the same. No difference can be seen in multiple runs.
Object versus array.Here is an interesting benchmark in Google Chrome. The first program assigns 100 integer properties to an object. The second program assigns 100 elements in an array.
Result: It is faster to use integer properties on an object in Chrome. The compiler optimizes properties better than array elements here.
Object.keys benchmark.Suppose you want the keys to an object. It seems like Object.keys can get these fast, but in this benchmark we see this approach has a cost.
Program 1: This version calls Object.keys on a small object in a tight loop. It checks the length.
Program 2: This version uses just one call to Object.keys and reuses that collection in the loop.
Result: The Object.keys method has a significant cost. It is far faster to cache the result of this method.
And: Features like constructors and methods are optional. A class in its simplest form is a dictionary (or hash, or map).
Object versus switch.An object can be used to replace a series of if-statements or a switch. In testing, though, a switch appears to execute faster in browsers.function Lookup
Return object.We can return an object literal when we need to return multiple values from a function. The syntax for returning multiple values is clear.Multiple Return Values
So: No string lookups are needed as properties are accessed. This makes property accesses on objects much faster.
A review.We can access object properties directly or with a string lookup by name. Browsers optimize both these syntax forms and treat them the same.
Dot Net Perls
|© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, firstname.lastname@example.org.|