loading...

JS: Arrays || Artificial Arrays, which are better?

calin92540842 profile image PDS OWNER CALIN (Calin Baenen) ・1 min read

I need help deciding, Arrays or Artificial Arrays?

["Hi","How","Are","You",2,"-day?"][2]; // Are

or

{
    0: "Hi",
    1: "How",
    // etc...
}[2]; // Are

So I want these to be considered:

  • Performance
  • Memory
  • Accessibility

Let me know what you think is better.
Anyways, thanks for reading! :)

Discussion

markdown guide
 

So arrays are implemented differently to objects in V8 for instance, iterating arrays is clearly much faster than iterating objects. If you have numeric indices then an array makes a lot of sense. If you want to work over the majority of the contents of the collection, then arrays also make more sense.

If you want to look something up with a string value then objects are very fast - much faster than searching an array for the item. An array index lookup should be faster than an object lookup because the process to get a value from an object is to convert the key into a hash, in the case of an array the number is a direct reference into the collection of members.

If you want to use a key that is not a string then use a Map.