How can I send large arrays through ports?

It may also be worth pointing out that an Elm Array is not implemented as a plain JS Array.

An Elm Array is a functional data structure which is immutable. To avoid copying the entire array on every Array.set, it is actually structured as a tree (with a high branching factor). This means that updates to the array can alter just one branch of the tree, but much of the new version of the tree can share the same memory as the original.

So when you pass a JS array into a port and decode it as an Elm array, it won’t simply pass through unchanged. The JS array will be restructured into an Elm array.

Just thought you might like to know in case you were thinking that using an array would carry zero performance cost.

3 Likes