I benchmarked Bytes-over-Ports workarounds

I have struggled multiple times with the inability to send elm/bytes over ports in Elm.
Searching for solutions I have seen multiple ways to do it, each suboptimal in their own way.
Recently I wondered which method is the most performant and created GitHub - anmolitor/elm-bytes-ports-benchmark: Compares a the different available hacks to get bytes from JS into Elm and vice versa.

Before you click the link:

  • What methods do you know currently?
  • Which one do you expect to perform the best for small amounts of Bytes?
  • Which one do you expect to perform the best for large amounts of Bytes?

I was surprised how well a HTTP approach via patching the XMLHttpRequest prototype
performed. I expected it to do well, but only for large sizes. Apparently the overhead of creating a request/response object is lower than I thought.

What do you think about the method used for benchmarking? Are there any potential ways that I missed?

Thanks for reading and lets hope that in the Future there is a standard way for passing Bytes between the languages without overhead.

13 Likes

Patching XMLHttpRequest to get bytes over ports is such a hack though… and how do we recieve bytes through a port? Love Elm, but hate that this has been broken for soooo loooong…

2 Likes

The JS → Elm side for Http uses a normal port with null/unit type to tell Elm: hey please make a Http Request and then patches the request to return bytes from JS. So just like Elm → JS bit with an extra step. Still apparently faster than using File.decoder in all benchmarked cases.

2 Likes

Good work on setting up these benchmarks.

3 Likes

For my comprehension, if all you care about is sending bytes in an http message (think web-grpc style protobuf encoded data) then does one need to care about this? I.e. is the focus here in getting bytes over ports specifically?

Yeah exactly. If you can work without ports, just do that, it will have the best possible performance, and needs no XMLHttpRequest monkeypatch.

2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.