Disclaimer: I am ReasonML beginner.
I have started playing with ReasonML lately and I have noticed a big difference in performance in contrast to vanilla JavaScript. Here's my examples against a simple puzzle solving function (puzzle taken from: https://adventofcode.com/2015/day/1)
ReasonML
let head = str =>
  switch (str) {
  | "" => ""
  | _ => String.sub(str, 0, 1)
  };
let tail = str =>
  switch (str) {
  | "" => ""
  | _ => String.sub(str, 1, String.length(str) - 1)
  };
let rec stringToList = str =>
  switch (str) {
  | "" => []
  | _ => [[head(str)], tail(str) |> stringToList] |> List.concat
  };
let rec findFloor = code =>
  switch (head(code)) {
  | "" => 0
  | "(" => 1 + findFloor(tail(code))
  | ")" => (-1) + findFloor(tail(code))
  | _ => 0
  };
let findFloorFold = code => {
  let calc = (a, b) =>
    switch (b) {
    | "" => a
    | "(" => a + 1
    | ")" => a - 1
    | _ => a
    };
  code |> stringToList |> List.fold_left(calc, 0);
};
JavaScript
const solve = code => {
  let level = 0;
  for (let i = 0, l = code.length; i < l; i++) {
    const el = code[i];
    if (el === "(") {
      ++level;
      continue;
    }
    if (el === ")") {
      --level;
      continue;
    }
  }
  return level;
};
Results
- JavaScript: 5ms
- ReasonML (recursive): 470ms
- ReasonML (non-recursive): 7185ms
Is this expected or are my ReasonML function implementations too slow?
Thanks in advance for clarifications/suggestions.
Admittedly the second solution (non-rec) is slow due to the string to array conversion but this is because in ReasonML string is not represented by a list of chars.
 
    