ES6 features you should start using
I know it has been a while since ES6 is out, but I realize even in 2023 that some of these features of javascript are not widely used or adopted.
Map
We all know what an object is in Javascript but how much do we know about Map ?
Because JavaScript is dynamically typed and lets you add or delete attributes whenever you want, an object acts like a dictionary. And Map ?
- Map provides get, set, has, and delete methods
- Accepts any type for the keys instead of just strings
- Provides an iterator for easy for-of usage and maintains the order of results
- Doesn’t have edge cases with prototypes and other properties showing up during iteration or copying
- Supports millions of items
- It s very fast
However, objects can be a better option if you’re solely utilizing string-based keys and want the fastest read performance. This is because the access path for properties is significantly faster than a function call for Map, and JavaScript engines compile objects down to C++ classes in the background Map().get().
These classes are also cached, so if you create a new object with the exact same characteristics as an existing background class, the engine will reuse it. The form of the class changes when a property is added or removed, which requires the backing class to be recompiled and makes utilizing an object as a dictionary with numerous additions and deletions quite slow. However, reading existing keys without modifying the object is very quick.
If you have a write-once read-heavy workload with string keys then you can use an
object
as a high-performance dictionary, but for everything else use aMap()
.
Set
First of all, we have to clarify this, what is a Set and what is an Array?
In general, an array is a type of structure that holds a block of data (such as objects, numbers, etc.) that has been allocated to consecutive memory.
Set, more often known as a mathematical notion, is an abstract data type that only includes unique components or objects and doesn’t need their allocation to be in any particular order by index.
The biggest difference here is that the Array elements can be duplicated, but in a Set they are all unique.
Another key difference is that Array is a collection that, the data ordered by an index value. On the other hand, Sets are data collections that are ordered by key, not index.
WeakMap and WeakSet
WeakMap
and WeakSet
are two built-in data structures in JavaScript that are similar to Map
and Set
, but with some important differences.
WeakMap
: A WeakMap
is a collection of key-value pairs where the keys must be objects and the values can be arbitrary values. The main difference between a WeakMap
and a Map
is that the keys in a WeakMap
are weakly held, which means that they are not referenced strongly, and can be garbage collected if no other reference to the object exists. This makes WeakMap
useful for scenarios where you want to associate additional data with an object, but don't want to keep that object alive solely for the purpose of storing that data.
WeakSet
: A WeakSet
is a collection of objects where each object can only appear once in the collection. Like WeakMap
, the objects in a WeakSet
are weakly held, which means that they can be garbage collected if there are no other references to the object. This makes WeakSet
useful for scenarios where you want to keep track of a set of objects, but don't want to keep those objects alive solely for the purpose of being in the set.
Both WeakMap
and WeakSet
can be useful in certain scenarios, but they also have limitations. Since the keys and objects in WeakMap
and WeakSet
are weakly held, you cannot iterate over them, and there is no way to determine the size of the collection. Additionally, the keys in WeakMap
must be objects, and the objects in WeakSet
must be distinct from one another.