在io.js中使用smalloc [英] use of smalloc in io.js

查看:104
本文介绍了在io.js中使用smalloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

io.js 的第一个版本已于本月发布,我正在阅读文档时我发现 smalloc 是io.js中引入的新模块。



直到今天,我从未觉得需要在JavaScript中这样做。



我的问题是:



  1. 我想知道使用 smalloc
    javscript中是否真的需要原始内存分配?


  2. 如果需要,为什么呢?


  3. 使用 smalloc 的用例是什么?


  4. 如果没有,那么为什么io.js会员添加这个模块?



它还说



可能是指定外部数组数据的类型你想。所有可能的选项都列在 smalloc.Types



用法示例:

  var doubleArr = smalloc.alloc(3,smalloc.Types.Double); 

这里是支持分配的类型列表

  smalloc.Types#

Int8
Uint8
Int16
Uint16
Int32
Uint32
Float
Double
Uint8Clamped




< ol start =5>
  • 我们是否尝试将javascript设为强类型语言



  • 解决方案

    首先,缓冲区由smalloc模块支持,此模块未由 io.js devs添加,它是在节点0.11 branch, io.js 刚刚导入它。原始内存分配意味着较低级别的内存操作,因此 - 更快的操作,更好的性能,这是 node.js io.js的目标。因此,如果您需要在二进制世界中实现某些内容而不限于当前的Buffer API,则应使用smalloc创建自己的方法来操作内存。正如文档所说:


    这可以用来创建自己的类缓冲类。没有设置其他属性,因此用户需要跟踪其他必要信息(例如分配的长度)。


    ,这不是尝试使javascript成为强类型语言,这只是内存操作,不能以其他方式完成,以确保更高的性能。


    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?

    解决方案

    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.

    这篇关于在io.js中使用smalloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆