Arrays with gaps in their indicators entail any benefits that compensate for their shortcomings

Javascript arrays can have spaces in their indices, which should not be confused with elements that are simply undefined :

 var a = new Array(1), i; a.push(1, undefined); for (i = 0; i < a.length; i++) { if (i in a) { console.log("set with " + a[i]); } else { console.log("not set"); } } // logs: // not set // set with 1 // set with undefined 

Since these spaces damage the length property, I'm not sure if they should be avoided when possible. If this is the case, I would consider them as a boundary case, not the default:

 // default: function head(xs) { return xs[0]; } // only when necessary: function gapSafeHead(xs) { var i; for (i = 0; i < xs.length; i++) { if (i in xs) { return xs[i]; } } } 

Besides the fact that head is very concise, another advantage is that it can be used for all data types of array type. head is just a simple example. If such gaps need to be taken into account throughout the code, overhead should be significant.

+5
source share
2 answers

This can occur in any language that overloads hash tables to provide what is colloquially called an "array". PHP, Lua and JavaScript are three such languages. If you depend on the strict sequential behavior of a number array, then this will be an inconvenience for you. In general, behavior also provides convenience.

A classic algorithm is defined here: remove an element from the middle of the data structure, which data structure is โ€œbetterโ€: a linked list or an array?

You have to say โ€œlinked listโ€ because removing the node from the linked list does not require you to move the rest of the array one index at a time. But linked lists have other pitfalls, is there also a different data structure that we can use? You can use a sparse array *.

In many languages โ€‹โ€‹that provide this hash type of arrays, deleting any arbitrary member of the array will change the length. Unfortunately, JavaScript does not change length, so you lose a little there. But, nevertheless, the array is "shorter", at least from the point of view of Object.keys .

* Many sparse arrays are implemented using linked lists, so do not use this as a whole. However, in these languages โ€‹โ€‹they are hash tables with predictable ordered numeric keys.

+2
source

Of course, the question is subjective, but I argue that, if possible, spaces should be avoided. Arrays are special Javascript objects with very specific goals. You can hack arrays completely, control the length property, add properties using keys other than numbers (for example, myArray ["foo"] = "bar"), but they are mostly passed to antipatterns. If you need a special pseudo-array form, you can always simply encode it yourself using a regular object. After all, typeof [] === "object"

It doesn't seem like spaces initially break your code, but I would avoid them intentionally.

Does this answer your question?

+1
source

Source: https://habr.com/ru/post/1240025/


All Articles