Reference Equality vs Structural (deep) Equality

Nik Graf
InstructorNik Graf
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 years ago

In this lesson we explore what structural equality as well as referential equality means in ReasonML. We walk through a couple examples and end with a handful of ideas on what to use when.

Instructor: [00:00] To compare if two values have the same content, we use two equal signs. This is called structural equality. It works with all sorts of data structures, Booleans, tuples, lists, records, and so on.

[00:32] Structural equality in Reason does a deep comparison. Therefore, we can compare the nested structures, as well. Here, for example, comparing a tuple containing a list with one record.

[00:52] Using a hash bang and an equal sign checks if two values aren't structurally identical. Referential equality can be checked using three equal signs.

[01:04] This operator doesn't compare the content, but rather if both values point to the same representation in memory. When binding a value to a name and comparing the name with itself, it is the same reference and results in true.

[01:17] Here, a comparison of structural equality to referential equality. What to use now?

[01:28] Most of the time, you will want to use structural equality. The only very few cases where referential equality is useful, one would be comparing mutable props inside shouldComponentUpdate of a React component.

[01:42] It would be a quick and cheap check, but also might miss if a value with [?] this identical content is passed in. In comparison, a lot of deep structural equality checks on large structures could possibly slow down your rendering.

[01:56] As said, in general, prefer structural equality. My shouldComponentUpdate example is quite a match case, and unless you need 60 frames per second, even structural equality might be fine.

[02:09] In addition, referential equality has its gotchas. For example, comparing two integers referentially will always result in true to how they are implemented.

[02:20] Even more so, stay away from referential inequality. It only implies two things are not the same representation in memory. I can't even think of a useful use case.

egghead
egghead
~ 7 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