Wednesday, June 18, 2008

Javascript Malloc and Free

There is no byte sized objects in Javascript. Numbers are 64bit floats and characters are 16bit unicode characters. I have been working on a way to give c developers in a javascript world fine grained control over memory usage. Right now I have two seperate implementations of my memory store. One is based on a massive string and is very efficient in size and reading bytes but it alittle slow at setting bytes. The other is based on a array of numbers. This one is not as efficient but much faster for reading and writing memory. Benchmarks show the array based store can allocate a megabyte worth of memory and set each byte to a value in about six seconds.

Here is a example class that is backed by the memory store:



JMM.ASCIIString = Class.extend({
construct : function (mmu,value){
this.mem = mmu.malloc(value.length);
for(n=0;n<value .length;n++)
{
this.mem.setValue(n,value.charCodeAt(n));
}
},
toString : function(){
str="";
for(n=0;n<this.mem.size;n++)
{
str += String.fromCharCode(this.mem.getValue(n));
}
return str;
}
});

1 comment:

  1. I know this comment is kind of out of place, but I stumbled on your blog when looking for proof of Wormhole existence, and that I didn't just dream the game up.

    Kudos to you for putting words in about it. Where did you get that little pic from?

    ReplyDelete