The first release for io.js is out this month, I was reading the docs when I found smalloc a new module introduced in io.js.
Till today I have never felt a need of doing so in JavaScript.
My questions are:
I wonder if there is really a need of raw memory allocation in
javscript using smalloc
?
If its needed then why?
what would be the use case for using smalloc
?
and if not then why did io.js members add this module?
It also says
It's possible is to specify the type of external array data you would like. All possible options are listed in smalloc.Types
.
Example usage:
var doubleArr = smalloc.alloc(3, smalloc.Types.Double);
and here is the list of types supported for allocation
smalloc.Types#
Int8
Uint8
Int16
Uint16
Int32
Uint32
Float
Double
Uint8Clamped
- Are we trying to make javascript a strongly typed language?
First of all, buffers are backed by the smalloc module and this module was not added by io.js
devs, it was initiated in node 0.11
branch, io.js
just imported it. Raw memory allocation means a lower level of memory manipulation and thus - faster operations, better performance, which is what aims both node.js
and io.js
. So if you need to implement something in the binary world without being limited to current Buffer API you should use smalloc to create your own ways to manipulate the memory. As the docs say:
This can be used to create your own Buffer-like classes. No other properties are set, so the user will need to keep track of other necessary information (e.g. length of the allocation).
Also, this is not a try to make javascript a strongly typed language, this is just memory manipulations, it can't be done in other way ensuring higher performance.
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.