To get as close as possible to raw memory from JavaScript we have to use WebGL typed arrays. It’s easy for a JIT to befriend those guys and optimize the hell out of reads and writes, because they have a nice semantics for their backing stores: no nasty holes leading to prototype lookups, all elements have known primitive type and no boxing is required.
var blocks = []; function Block(size) { this.size = size; this.buf = new ArrayBuffer(this.size); this.i32 = new Int32Array(this.buf); this.f64 = new Float64Array(this.buf); } function malloc(N) { if (blocks[N] && blocks[N].length) return blocks[N].pop(); return new Block(N); } function free(addr) { (blocks[addr.size] || (blocks[addr.size] = [])).push(addr); }This is a very naïve implementation (I would even say parody) of the famous
malloc&free
duo. It does not try to optimize memory usage at all but it is perfect for our demonstration.
via blog.mrale.ph
I need to rewrite my Javascript Opendap server using WebGL typed arrays.