Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of smalloc in io.js

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:

  1. I wonder if there is really a need of raw memory allocation in javscript using smalloc?

  2. If its needed then why?

  3. what would be the use case for using smalloc?

  4. 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
  1. Are we trying to make javascript a strongly typed language?
like image 263
Naeem Shaikh Avatar asked Jan 21 '15 11:01

Naeem Shaikh


1 Answers

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.

like image 185
micnic Avatar answered Oct 18 '22 06:10

micnic