This article is part of a web development series from Microsoft. Thank you for supporting the partners who make SitePoint possible.
Most major programming languages have several types of data collections. Python has lists, tuples, and dictionaries. Java has lists, sets, maps, queues. Ruby has hashes and arrays. JavaScript, up until now, had only arrays. Objects and Arrays were the workhorses of JavaScript. ES6 introduces four new data structures that will add power and expressiveness to the language: Map
, Set
, WeakSet
, and WeakMap
.
Let's examine these four new collections and the benefits they provide.
Searching for the JavaScript HashMap
HashMaps, dictionaries, and hashes are several ways that various programming languages store key/value pairs and these data structures are optimized for fast retrieval.
In ES5, JavaScript objects–which are just arbitrary collections of properties with keys and values–can simulate hashes, but there are several downsides to using objects as hashes.
Downside #1: Keys must be strings in ES5
JavaScript object property keys must be strings, which limits their ability to serve as a collection of key/value pairs of varying data types. You can of course coerce/stringify other data types into strings, but this adds extra work.
Downside #2: Objects are not inherently iterable
Objects weren’t designed to be used as collections, and as a result there’s no efficient way to determine how many properties an object has (see, for e.g., Object.keys is slow). When you loop over an object’s properties, you also get its prototype properties. You could add the iterable
property to all objects but not all objects are meant to be used as collections. You could use the for...in
loop and the hasOwnProperty()
method, but this just a workaround. When you loop over an object’s properties, the properties won’t necessarily be retrieved in the same order they were inserted.
Downside #3: Challenges with built-in method collisions
Objects have built in methods like constructor
, toString
, and valueOf
. If one of these was added as a property, it could cause collisions. You could use Object.create(null)
to create a bare object (which doesn't inherit from object.prototype
), but, again, this is just a workaround.
ES6 includes new collection data types, so there is no longer a need to use objects and live with their drawbacks.
Using ES6 MapCollections
Map is the first data structure/collection we’ll examine. Maps are collections of keys and values of any type. It’s easy to create new Maps, add/remove values, loop over keys/values and efficiently determine their size. Here are the crucial methods:
Creating a map and using common methods
Code Example: http://ift.tt/29w3wzZ
Using the SetCollection
Sets are ordered lists of values which contain no duplicates. Instead of being indexed like an arrays are, sets are accessed using keys. Sets already exist in Java, Ruby, Python, and many other languages. One difference between ES6 Sets and those in other languages is that the order matters in ES6 (not so in many other languages). Here are the crucial Set methods:
Code Example: http://ift.tt/29xpP4H
Weak Collections, Memory, and Garbage Collections
JavaScript Garbage Collection is a form of memory management whereby objects that are no longer referenced are automatically deleted and their resources are reclaimed.
Continue reading %Using the New ES6 Collections: Map, Set, WeakMap, WeakSet%
by Kyle Pennell via SitePoint
No comments:
Post a Comment