⚠️ This lesson is retired and might contain outdated information.

Lightning Fast Immutable.js Equality Checks with Hash Codes

J.S. Leonard
InstructorJ.S. Leonard
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 2 years ago

While Immutable.js offers .is() to confirm value equality between iterables it comes at the cost of referencing each key and value in both objects. For lightning fast equality checks, Immutable.js can produce a hash code based on an iterable's content. If two iterables have the same content, their hash codes will be the same. It's worth noting that this technique is unsuitable for mission critical application development since there is a chance, however slight, that checksums like these might collide. This is outlined here: https://en.wikipedia.org/wiki/Collision_(computer_science)

[00:00] I want to show you a neat little technique that I stumbled upon when trying to compare two large data sets. While Immutable.js offers is to confirm value equality between the iterables, it comes at the cost of referencing each key in value in both objects.

[00:16] For lightning fast equality checks, immutable.js can produce a hash code based on an iterable's content. If two iterables have the same content, their hash codes will be the same. It's worth noting the technique is unsuitable for mission-critical application development. Since there is a chance, however slight, that check sums like these might collide. This is outlined here in their documentation and points to Wikipedia.

[00:41] Let's take a look in how to do this since it's such a useful technique. I've got my To Do class. I've got this function that will generate To Do's. We're going to go ahead and create two different immutables, two different immutable lists. We're going to compare them.

[00:57] Let's do VaR. To Do's equals generate To Do's. We've got a series of To Do's being pushed to an array. We can create out iterables based off this now. We'll do, let To Do's1 equals immutable.list.of and To Do's. We'll do the same thing for another one.

[01:24] To Do's 2. We should expect these are two different objects, they might not equal each other. Expect To Do's1 to not equal ToDos2 because these are holding different places in memory and that's pretty much what that's checking. They're two different objects.

[01:44] However, they are, for all intents and purposes, equal. Is would work here, but we'd also come at a performance cost if we generated quite a few To Do's. What we're going to do is this. We're going to do, expect To Do's1.hashcode. It's going to generate a hash code for To Do's1. It checks them. I want to see if that is equal to To Do's 2.hashcode. We spelled that correctly.

[02:18] Sure enough, we have it. You can actually cache this hash code now and reference it for future objects. It's a super fast way for checking things.

egghead
egghead
~ 6 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today