Skip to content
Merged
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
43 changes: 31 additions & 12 deletions pages/docs/manual/v12.0.0/variant.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -762,13 +762,16 @@ Performance-wise, a variant can potentially tremendously speed up your program's

```js
let data = 'dog'
if (data === 'dog') {
...
} else if (data === 'cat') {
...
} else if (data === 'bird') {
...
function logData(data) {
if (data === 'dog') {
...
} else if (data === 'cat') {
...
} else if (data === 'bird') {
...
}
}
logData(data)
```

There's a linear amount of branch checking here (`O(n)`). Compare this to using a ReScript variant:
Expand All @@ -778,16 +781,32 @@ There's a linear amount of branch checking here (`O(n)`). Compare this to using
```res example
type animal = Dog | Cat | Bird
let data = Dog
switch data {
| Dog => Console.log("Wof")
| Cat => Console.log("Meow")
| Bird => Console.log("Kashiiin")
let logData = data => {
switch data {
| Dog => Console.log("Wof")
| Cat => Console.log("Meow")
| Bird => Console.log("Kashiiin")
}
}
logData(data)

```
```js
console.log("Wof");
function logData(data) {
switch (data) {
case "Dog" :
console.log("Wof");
return;
case "Cat" :
console.log("Meow");
return;
case "Bird" :
console.log("Kashiiin");
return;
}
}

var data = "Dog";
logData("Dog");
```

</CodeTab>
Expand Down