Thanks @micnic for answering the question well. I would like to offer some supplemental information about why I implemented smalloc.
Don't think raw memory allocations in JS are some strange new thing. It's the same type of mechanism that's used by Typed Arrays under the hood. So, anywhere you can use a Typed Array you could also use smalloc. The advantage of smalloc is that it doesn't define anything for you. Allowing the maximum flexibility of your API. It's also safe, because the GC will clean up your allocations when the object is no longer being used.
One usage would be for a math library. Especially if writing a native module. I personally use it for tricky performance optimizations of allocating memory on an object then sharing that memory between JS and C++ to enable shared state between the two. It's by far the fastest way to do so, and has lead to some impressive optimizations in Node and io.js.
Remember that you can allocate onto existing objects. Which is where the power lies. For example:
function Alloc(n) {
n >>>= 0; // uint32 conversion
this.length = n;
smalloc.alloc(n, this);
}
var a = new Alloc(16);
There's a simple new construct that just allocates a Uint8
external data array onto the instance.
I'll quickly reiterate answers to your questions:
- I wonder if there is really a need of raw memory allocation in javscript using smalloc?
Yes. Think Typed Arrays.
- If its needed then why?
Answered above. Also, search for anything that uses Typed Arrays.
- what would be the use case for using smalloc?
Answered above. In addition, there are many other uses that developers are finding for it.
- and if not then why did io.js members add this module?
I wrote it long before io.js was around. :)
- Are we trying to make javascript a strongly typed language?
Absolutely no. The two are not even related.
UPDATE: Because of breaking changes coming in V8 v4.4, smalloc
has been marked "deprecated" starting in io.js v2.