One of the first thing new JavaScript programmers learn is hashes. Hashes are useful for saving data, nesting data, and in general having a faster Big-O lookup time than arrays*.
*Do you know the reason that hashes have O(1) lookup time? IT'S BECAUSE A HASH IS A FUNCTION! You take in your hash keys, do a bunch of fancy math, and out pops a singular result for each key input. It still sort of blows my mind that a hash is essentially the sort of thing we studied in Algebra 1, only you can input strings, numbers, whatever you want into a hash and it'll still spit out an answer. Of course, all those strings and s and whatnot are of course binary numbers, so it makes a fair amount of sense when you think about it.
If you had Aleph-One amount of memory, you could make a hash table for these functions.
Hashes are useful for all sorts of things as well - the sorts of things you might encounter in code challenges in particular, like counting up how many of a thing there are in an array, or doing something every time something happens in an array, or...basically making an array cooler.
let array = [1,1,3,2,0,2,3,3]
let hash = {}
for (let i=0; i < array.length; i++) {
if (!hash[array[i]]) {
hash[array[i]] = 1
} else {
hash[array[i]]++
}
}
console.log(hash)
//this code counts up how many of each number there are in the array. See the result below:
Hashes are certainly useful, but they have some limitations. The numbers in the hash above, for instance, are listed in numerical order. Not because they're actually ordered, but because that's how the console prints them out. This can be really frustrating if, for instance, you want to find out what key was the first one made for your hash, or you wanted to order your keys and take them out later.
Enter
hashmaps.
A hashmap is exactly like a hash, except that it is ordered. It's a bit like a hash plus an array. So you can do things like spit out your ordered pairs in the order they were entered:
let array = ["c","c","1","b","z","b","a",'a']
let map = new Map
for (let i=0; i < array.length; i++) {
if (!map.has(array[i])) {
map.set(array[i], 1);
} else {
map.set(array[i], map.get(array[i]) + 1);
}
}
console.log(map)
for (const [key, value] of map.entries()) {
console.log(key, value);
}
console.log("map size:", map.size)
What do we get when we run the above code?
Not only does JavaScript not re-order the hashmap entries when printing them out, it also actually keeps that order in its internal structure. And, very importantly, that ordering allows them to be iterable - you can do a for-loop with a hashmap, or a .forEach, or whatever iterable you want. Not only that, but you can check the size of the hashmap! So very useful, for instance, if you want to know how many keys you have, cap the number of keys you're storing, only iterate up to a certain key, etc.
There's also one other type of object in JavaScript that can be used similarly to hashes: sets.
let array = ["c","c","1","b","z","b","a",'a']
let set = new Set(array)
console.log(set)
Sets are objects that keep only unique values. So the set above simply keeps the unique objects in the array:

And, as you can see in that single line where we define the set, sets are pretty slick - it doesn't take a lot of code to make one. So instead of using setting up a hash like this:
let hash = {}
for (let i=0; i < array.length; i++) {
if (!hash[array[i]]) {
hash[array[i]] = true
}
}
to do things like be able to check if your data is new, you can simply make a set and be done with it.
Sets, like maps, also have a size. So you can do things like find out how many unique elements are in an array very quickly:
let array = ["c","c","1","b","z","b","a",'a']
let set = new Set(array)
console.log("The number of unique values in the array is:", set.size)
Doing this with a hash is much more involved:
let array = ["c","c","1","b","z","b","a",'a']
let hash = {}
let count = 0
for (let i=0; i < array.length; i++) {
if (!hash[array[i]]) {
hash[array[i]] = true
count++
}
}
console.log("The number of unique values in the array is:", count)
So, there you go: the world is much bigger than just hashes and arrays.
Comments
Post a Comment