[erlang-questions] Erlang in the browser

Jakob Praher <>
Tue Jan 31 12:44:41 CET 2012

Hi Fredirk,

nice work! 

Regarding representing data types. It might be worth to look at emscripten (https://github.com/kripken/emscripten).

Alon Zakai (the author) uses native memory layout for data structures. Native memory is represented as a arrays (optionally separated between primitive types) in Javascript.
According to his evaluations this is a very efficient representation technique. One could also investigate in taking the existing implementation of erl and compile it on LLVM to be used by emscripten.

For interfacing with Javascript (the downside of the data structures as native blobs) ints and floats can be used as is, but other data structures are just slieces in arrays. So it also depends on how much interaction is happening between erlang and javascript for this to be more efficient or not.

See: https://github.com/kripken/emscripten/wiki/Interacting-with-code

In preamble.js there are functions for setting and getting values on/from the heap.  For instance the function intArrayFromString that shows how the conversion between a Javascript string and the heap C-like null-terminated char array is done.


PS: For my resarch I have prototyped a Prolog interpreter in Javascript (for educational reasons only). In this implementation I used Javascript arrays for functors. I also thought about marking arrays as being either native arrays or functors.

Am Dienstag, 31. Januar 2012 11:56 CET, Fredrik Svahn <> schrieb: 
> Hi Joe,
> The Erlang list [1, 2, 3] is represented as js native objects {value:
> 1, next: {value: 2, next: {value: 3, next:nil}}}. The size of a string
> represented as a list is 40 bytes per character in Chromium on my
> x86_64 machine (I do not know if the size would be smaller on a 32 bit
> machine). The corresponding number for beam is 16 bytes/char for
> x86_64, I believe, so it is more wasteful as expected. I have
> considered mapping it to js arrays but haven't done any measurements.
> This is just a small script so I did not want to write my own GC, also
> mapping to native objects is better if I later would like to make it
> possible to compile performance critical beams to native javascript
> code.
> The Erlang tuple {1,2,3} is represented as the js array [1, 2, 3].
> Binaries are mapped to js strings, and atoms to numbers.
> BR /Fredrik
> On Tue, Jan 31, 2012 at 10:24 AM, Joe Armstrong <> wrote:
> > Wow - ultra interesting.
> >
> > Just out of curiosity - how do you represent lists and tuples in JS?
> > Are they represented
> > as  native JS objects or as objects on the beam stack and heap? And if
> > they are native JS objects what is the storage overhead of a
> > list-cell?
> >
> > /Joe
> >
> >
> > On Mon, Jan 30, 2012 at 10:14 PM, Fredrik Svahn <> wrote:
> >> There is also http://svahne.github.com/browserl, a small script which
> >> I wrote just for fun.
> >>
> >> Short version: it is an Erlang emulator in javascript, which can load
> >> and run an Erlang/OTP system from a tar file with ordinary beam files.
> >> It can also load and execute individual beam files. It will run common
> >> test and successfully complete some OTP test suites (the test suites
> >> are not loaded in the example page, though). Files, binaries, ets and
> >> floats are work in progress, at the moment they are only supported to
> >> the extent needed to boot the system and run common test.
> >>
> >> BR /Fredrik
> >>
> >>
> >> On Tue, Nov 29, 2011 at 12:47 AM, Raoul Duke <> wrote:
> >>> wow.
> >>> _______________________________________________
> >>> erlang-questions mailing list
> >>> 
> >>> http://erlang.org/mailman/listinfo/erlang-questions
> >> _______________________________________________
> >> erlang-questions mailing list
> >> 
> >> http://erlang.org/mailman/listinfo/erlang-questions
> _______________________________________________
> erlang-questions mailing list
> http://erlang.org/mailman/listinfo/erlang-questions

More information about the erlang-questions mailing list