JavaScript Objects built on arrays [duplicate] - javascript

This question already has answers here:
Why can I add named properties to an array as if it were an object?
(8 answers)
Difference between array and object in javascript? or Array Vs Object
(4 answers)
Associative array versus object in JavaScript
(7 answers)
Closed 4 years ago.
From my readings, in JavaScript:
Objects = Hash Tables, which are build on Arrays. However, it is commonly said that Arrays are Objects in JS. How are these two concepts reconciled?

Objects are not built on arrays. Objects have their own optimizations.
In general:
Objects are for "structs", structures of predictable "shape" and keys known in advance (even though they can be used with dynamic keys, you should use Maps for that. See below).
Arrays are for lists (and queues, and stacks), structures where the keys are numbers, or where the order of elements matters. Arrays are "special" objects, not the other way around. (You can put string-based properties on an array, just like any object. Please don't do that though).
Maps are for hash tables/dictionaries, structures where the keys are dynamic and not known in advance.

Related

Are arrays objects in JavaScript? [duplicate]

This question already has answers here:
Are Javascript arrays primitives? Strings? Objects?
(7 answers)
Closed 3 years ago.
I have had discussions with seasoned programmers who do not think arrays are objects; they think that objects and arrays are of separate types of the data structure category. But, as far as I understand, objects and arrays are of the same type.
If you were to use the unary typeof operator to evaluate a binding storing an array, the JavaScript interpreter will return the string "object." Moreover, you can use the Object.keys() method on a binding containing an array to get the property names of the array object.
When studying programming, I see authors use the phrase "objects and arrays," as if these were separate entities, which adds to the confusion. I go to W3Schools, and in the section "Data Types," they list arrays and objects as if they are not of the same data type.
As far as I understand it, objects and arrays are the same, except array objects are structured different than objects delimited by braces. Perhaps I'm missing something and can be enlightened.
Are arrays objects? If so, shouldn't they be listed as the same type, and shouldn't the phrase be "arrays and other objects"? If not, why not?
(Edit: I know the question of if objects and arrays are the same has been asked, but I was also asking if there should be a change in how arrays and objects are discussed, so people don't think that arrays are not objects, since programming is not the easiest subject to digest.)
Arrays are indeed a type of object.
console.log([] instanceof Object);
I go to W3Schools, and in the section "Data Types," they list arrays and objects as if they are not of the same data type
W3Schools is not a particularly trustworthy source. That said, when discussing organization of data, it's pretty common to talk about arrays (an ordered collection of values) as distinct from objects (a usually-unordered collection of key-value pairs), despite the fact that one is a subtype of the other, because the fact that arrays inherit from Object.prototype often isn't something one needs to consider when organizing / sorting through data.
If one tries to use any of the Object methods on an array, it's certainly essential to remember that Array inherits from Object, but usually that's not necessary - often, one will be only be using array methods instead (like forEach, find, includes, etc).

Does the length of an object's property names impact memory usage? [duplicate]

This question already has an answer here:
Do browser engines compress keynames in large arrays of reoccurring objects?
(1 answer)
Closed 5 years ago.
I have array of objects containing around 200000 records. The objects have long key names. Will shorter property names consume less memory? Will it be enough to make a difference in performance?
If I prepare my data like:
[{"dTN":"datatype value","dTR":"dataType range"},....]
instead of:
[{"dataTypeName":"datatype value","dataTypeRange":"dataType range"},....]
how it will effect memory usage in browsers?
Can anyone please explain how javascript handles object property names?
You can check using window.performance.memory in Chrome.
If you have many repeating strings I believe they will be optimized, this completely depends on the engine of course. If the keys are all different you might see an increase.

What is the difference between a dictionary and a Map in Javascript 6? [duplicate]

This question already has answers here:
Map vs Object in JavaScript
(15 answers)
Closed 7 years ago.
How is a Map different from a dictionary/object?
In other words, what is the difference between let x = {} and let x = new Map() ?
Objects and maps compared (from MDN):
Objects are similar to Maps in that both let you set keys to values,
retrieve those values, delete keys, and detect whether something is
stored at a key. Because of this (and because there were no built-in
alternatives), Objects have been used as Maps historically; however,
there are important differences between Objects and Maps that make
using a Map better:
An Object has a prototype, so there are default keys in the map.
This could be bypassed by using map = Object.create(null) since ES5,
but was seldomly done.
The keys of an Object are Strings and Symbols, where they can be
any value for a Map.
You can get the size of a Map easily while you have to manually
keep track of size for an Object.
This does not mean you should use Maps everywhere, objects still are
used in most cases. Map instances are only useful for collections, and
you should consider adapting your code where you have previously used
objects for such. Objects shall be used as records, with fields and
methods. If you're still not sure which one to use, ask yourself the
following questions:
Are keys usually unknown until run time, do you need to look them up dynamically?
Do all values have the same type, and can be used interchangeably?
Do you need keys that aren't strings?
Are key-value pairs often added or removed?
Do you have an arbitrary (easily changing) amount of key-value pairs?
Is the collection iterated?
Those all are signs that you want a Map for a collection. If in
contrast you have a fixed amount of keys, operate on them
individually, and distinguish between their usage, then you want an
object.

When would you use an ES6 Map over an Object? [duplicate]

This question already has answers here:
Map vs Object in JavaScript
(15 answers)
Closed 7 years ago.
I've been looking at the new ES6 docs on MDN and I can't find a real world use for the Map object where a normal object wouldn't work. Does anyone have any use cases for Maps and explain why an object wouldn't work in that scenario?
MDN lists a number of important differences:
An Object has a prototype, so there are default keys in the map. [editor: the good old hasOwnProperty issue]
The keys of an Object are Strings, where they can be any value for a Map.
You can get the size of a Map easily while you have to manually keep track of size for an Object.
A Map iterates its elements in insertion order, whereas iteration order is not specified for Objects.
So a Map is finally an insert-ordered key-value store for Javascript, which additionally allows mapping any value to any value, instead of restricting keys to be strings. This can greatly simplify some code where ordering is important, or where objects or other complex data types need to be associated with other data.

How do I reverse() an associative array (object)? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Loop through associative array in reverse
I have a key-value pair javascript object.
How do I reverse() it? or loop it starting from the bottom.
jQuery available.
There is no hard specification or guarantee of determining the order of properties in a Javascript object. Most browsers go from what was added first to last, but as Elements order in a "for (… in …)" loop
describes, Chrome and Opera do not.
If order is important to you, then you must use an array, or have an array that lists the order in which the properties should be accessed when looping (from either direction).

Categories

Resources