Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion javascript/computer_science/hash_map_data_structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
There is a key difference between hashing and ciphering (encryption): reversibility.
Hashing is a one-way process. Using the above example, you can make a hash code from a name, but you cannot take a hash code and revert it back to a name. If you have a name `"Carlos"`, we can hash it to `"C"`. But it's impossible to reverse it from `"C"` back to its original form. You cannot know if it's `"Carlos"`, maybe it's `"Carla"` or `"Carrot"`. We don't know.

<div class="lesson-note lesson-note--tip" markdown="1">

Check failure on line 29 in javascript/computer_science/hash_map_data_structure.md

View workflow job for this annotation

GitHub Actions / Lint lesson/project files

Note boxes have appropriate headings

javascript/computer_science/hash_map_data_structure.md:29 TOP012/note-box-headings Note boxes have appropriate headings [Note box is missing a heading. Note boxes must start with a level 4 heading (####).] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP012.md

Hashing is very good for security. Given a password, you can save the hash of that password rather than the password's plain text. If someone steals your hashes, they cannot know the original passwords since they are unable to reverse the hash back to the password.

Expand Down Expand Up @@ -138,7 +138,7 @@

With our new function we will have different hash codes for the names `"Sara"` and `"raSa"`. This is because even if both names have the same letters, some of the letters appear in different locations. The hash code started to change because we are multiplying the old hash with every new iteration and then adding the letter code.

<div class="lesson-note lesson-note--tip" markdown="1">

Check failure on line 141 in javascript/computer_science/hash_map_data_structure.md

View workflow job for this annotation

GitHub Actions / Lint lesson/project files

Note boxes have appropriate headings

javascript/computer_science/hash_map_data_structure.md:141 TOP012/note-box-headings Note boxes have appropriate headings [Note box is missing a heading. Note boxes must start with a level 4 heading (####).] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP012.md

Notice the usage of a prime number. We could have chosen any number we wanted, but prime numbers are preferable. Multiplying by a prime number will reduce the likelihood of hash codes being evenly divisible by the bucket length, which helps minimize the occurrence of collisions.

Expand All @@ -156,7 +156,7 @@

Let's talk about our number of buckets. We don't have infinite memory, so we can't have an infinite amount of them. We need to start somewhere, but starting too big is also a waste of memory if we're only going to have a hash map with a single value in it. So to deal with this issue, we should start with a small array for our buckets. We'll use an array of size `16`.

<div class="lesson-note lesson-note--tip" markdown="1">

Check failure on line 159 in javascript/computer_science/hash_map_data_structure.md

View workflow job for this annotation

GitHub Actions / Lint lesson/project files

Note boxes have appropriate headings

javascript/computer_science/hash_map_data_structure.md:159 TOP012/note-box-headings Note boxes have appropriate headings [Note box is missing a heading. Note boxes must start with a level 4 heading (####).] https://github.com/TheOdinProject/curriculum/blob/main/markdownlint/docs/TOP012.md

Most programming languages start with the default size of `16` because it's a power of 2, which helps with some techniques for performance that require bit manipulation for indexes.

Expand All @@ -178,7 +178,7 @@

- The `load factor` is a number that we assign our hash map to at the start. It's the factor that will determine when it is a good time to grow our buckets array. Hash map implementations across various languages use a load factor between `0.75` and `1`.

The product of these two numbers gives us a number, and we know it's time to grow when there are more entries in the hash map than that number. For example, if there are `16` buckets, and the load factor is `0.8`, then we need to grow the buckets array when there are more than `16 * 0.8 = 12.8` entries - which happens on the 13th entry. Setting it too low will consume too much memory by having too many empty buckets, while setting it too high will allow our buckets to have many collisions before we resize the array.
The product of these two numbers gives us a number, and we know it's time to grow when there are more entries in the hash map than that number. For example, if there are `16` buckets, and the load factor is `0.8`, then we need to grow the buckets array when there are more than `16 * 0.8 = 12.8` entries - which happens on the 13th entry. Setting the `load factor` too low will consume too much memory by having too many empty buckets, while setting it too high will allow our buckets to have many collisions before we resize the array.

### Computation complexity

Expand Down
Loading