This is the latest docs version
Quick Links
  • -Overview
  • -Language Features
  • -JS Interop
  • -Build System
Documentation
Language Manual
Reference for all language features
ReScript & React
First class bindings for ReactJS
GenType
Seamless TypeScript integration
Reanalyze
Dead Code & Termination analysis
Exploration
Packages
Explore third party libraries and bindings
Syntax Lookup
Discover all syntax constructs
APIPlaygroundBlogCommunity
  • Playground
  • Blog
  • Twitter
  • GitHub
  • Forum
Language Manual
Overview
  • Introduction
  • Installation
  • Migrate to v11
  • Editor Plugins
  • Try
Language Features
  • Overview
  • Let Binding
  • Type
  • Primitive Types
  • Tuple
  • Record
  • Object
  • Variant
  • Polymorphic Variant
  • Null, Undefined and Option
  • Array & List
  • Function
  • If-Else & Loops
  • Pipe
  • Pattern Matching / Destructuring
  • Mutation
  • JSX
  • Exception
  • Lazy Value
  • Promises
  • Async / Await
  • Tagged templates
  • Module
  • Import & Export
  • Attribute (Decorator)
  • Reserved Keywords
  • Equality and Comparison
    • Shallow equality
    • Deep equality
    • Comparison
    • Performance of runtime equality checks
Advanced Features
  • Extensible Variant
  • Scoped Polymorphic Types
JavaScript Interop
  • Interop Cheatsheet
  • Embed Raw JavaScript
  • Shared Data Types
  • External (Bind to Any JS Library)
  • Bind to JS Object
  • Bind to JS Function
  • Import from / Export to JS
  • Bind to Global JS Values
  • JSON
  • Inlining Constants
  • Use Illegal Identifier Names
  • Generate Converters & Helpers
  • Browser Support & Polyfills
  • Libraries & Publishing
  • TypeScript
Build System
  • Overview
  • Configuration
  • Configuration Schema
  • External Stdlib
  • Pinned Dependencies
  • Interop with JS Build Systems
  • Performance
  • Warning Numbers
Guides
  • Converting from JS
Extra
  • Newcomer Examples
  • Project Structure
  • FAQ
Docs / Language Manual / Equality and Comparison
Edit

Equality and Comparison

ReScript has shallow equality ===, deep equality ==, and comparison operators >, >=, <, and <=.

Shallow equality

The shallow equality operator === compares two values and either compiles to === or a bool if the equality is known to the compiler. It behaves the same as the strict equality operator === in JavaScript.

Using === will never add a runtime cost.

ReScriptJS Output
let t1 = 1 === 1 // true
let t2 = "foo" === "foo" // true
let t3 = { "foo": "bar" } === { "foo": "bar"} // false

let doStringsMatch = (s1: string, s2: string) => s1 === s2

Deep equality

ReScript has the deep equality operator == to check deep equality of two items, which is very different from the loose equality operator like == in JavaScript.

When using == in ReScript it will never compile to == in JavaScript, it will either compile to ===, a runtime call to an internal function that deeply compares the equality, or a bool if the equality is known to the compiler.

ReScriptJS Output
let t1 = 1 == 1 // true
let t2 = "foo" == "foo" // true
let t3 = { "foo": "bar" } == { "foo": "bar"} // true

let doStringsMatch = (s1: string, s2: string) => s1 == s2

== will compile to === (or a bool if the compiler can determine equality) when:

  • Comparing string, char, int, float, bool, or unit

  • Comparing variants or polymorphic variants that do not have constructor values

== will compile to a runtime check for deep equality when:

  • Comparing array, tuple, list, object, record, or regular expression Re.t

  • Comparing variants or polymorphic variants that have constructor values

When using == pay close attention to the JavaScript output if you're not sure what == will compile to.

Comparison

ReScript has operators for comparing values that compile to the the same operator in JS, a runtime check using an internal function, or a bool if the equality is known to the compiler,

operatorcomparison
>greater than
>=greater than or equal
<less than
<=less than or equal

Comparison can be done on any type.

An operator will compile to the same operator (or a bool if the compiler can determine equality) when:

  • Comparing int, float, string, char, bool

An operator will compile to a runtime check for deep equality when:

  • Comparing array, tuple, list, object, record, or regular expression (Re.t)

  • Comparing variants or polymorphic variants

ReScriptJS Output
let compareInt = (a: int, b: int) => a > b
let t1 = 1 > 10
let compareArray = (a: array<int>, b: array<int>) => a > b
let compareOptions = (a: option<float>, b: option<float>) => a < b

Performance of runtime equality checks

The runtime equality check ReScript uses is quite fast and should be adequate for almost all use cases. For small objects it can be 2x times faster than alternative deep compare functions such as Lodash's _.isEqual.

For larger objects instead of using == you could manually use a faster alternative such as fast-deep-compare, or write a custom comparator function.

This repo has benchmarks comparing results of different libraries compared to ReScript's built-in equality function.

Reserved KeywordsExtensible Variant

© 2024 The ReScript Project

Software and assets distribution powered by KeyCDN.

About
  • Community
  • ReScript Association
Find us on