技术控

    今日:68| 主题:49270
收藏本版 (1)
最新软件应用技术尽在掌握

[其他] Exploring JavaScript: Typed Arrays

[复制链接]
森末 发表于 2016-10-17 10:47:37
122 2

立即注册CoLaBug.com会员,免费获得投稿人的专业资料,享用更多功能,玩转个人品牌!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Typed Arrays is a relatively new feature in JavaScript. They are designed to provide an easy way to work with binary data and structures, that was hardly possible before.
  Initially, Typed Arrays were introduced for WebGL APIs. The reason behind this is that converting and guessing the type of standard JavaScript array might be too slow, while C API which is backing up WebGL might access already allocated in memory typed array.
  Typed Arrays are part of ECMAScript 2015 (also known as ES6) specification.
  Browser support

     
Exploring JavaScript: Typed Arrays-1 (typed,arrays,exploring,celestial,exploring,exploring,strategy,exploring,es6,pdf)
    The main "problem" with browser support is IE9, which is still quite popular, as well as some browsers handling typed arrays slightly different:
  
       
  • IE10 (and IE 10&11 mobile) does not support Uint8ClampedArray   
  • Safari 5.1 does not support Float64Array   
  • Firefox 14 and under do not have DataView yet.   
  • In versions before Safari 6, Typed Arrays are much slower than Array Objects.   
  • ArrayBuffer has no slice method in IE 10.  
  Creating typed array

  Typed array creation is relatively easy:
  [code]var arr = new Uint16Array(10);[/code]  [code]arr[0] = 0xFFFF;[/code]  [code]console.log(arr[0]);[/code]  Argument we are passing to array constructor is the number of elements in array (in the example above we are creating array of 10 uint16 numbers, which will take 20 bytes of memory).
  Typed arrays could be following
   Int8Array
  size in bytes: 1
  description: 8-bit two's complement signed integer
  Web IDL type: byte
  C type: int8_t
  JavaScript Operators & Typed Arrays

  You might expect that all the operations on typed arrays will be done on bits this array contains. Apparently, that's not true.
  Extending previous example:
  [code]var arr = new Uint16Array(10);[/code]  [code]arr[0] = 0xFFFF;[/code]  [code]console.log(~arr[0]); // -65535[/code]  ArrayBuffer & DataView

  ArrayBuffer & DataView are parts of Typed Array implementation.
   ArrayBufferis basically holding the data of Typed Array.
  Per example:
  [code]var fileArrayBuffer = reader.readAsArrayBuffer(file);[/code]  Here, ArrayBuffer will be holding all the bytes of the file. Sure, file might be in one encoding or another. E. g. to read UTF-8 characters, we need to assign Uint16 array type to this buffer:
  [code]var arr = new Uint16Array(fileArrayBuffer);[/code]  Now we can access this data in a regular way:
  [code]String.fromCharCode([/code]   DataViewis a bit more interesting part of implementation which is providing to read structures kept in binary data.
  E. g., you have C structure like this:
  [code]struct data { unsigned int id; char[10] username;}[/code]  We can read it from js by doing something like this:
  [code]arr[0] = 0xFFFF;0[/code]  Here also alternative API can be used, e. g.:
  A couple of use cases

  WebGL

  Buffer, pixel data and texture mappings are all using typed arrays. Example for texture mapping:
  [code]arr[0] = 0xFFFF;1[/code]  Canvas

  Yes, canvas image data is typed array:
  [code]arr[0] = 0xFFFF;2[/code]  Useful to replace/analysing colors and for doing other kinds of image manipulation.
  WebSockets

  Yes, Web Sockets do support transferring binary data and ArrayBuffer representation of data. Enabling it is quiet easy:
  [code]arr[0] = 0xFFFF;3[/code]  Other Browser APIs

  File API, XMLHttpRequest, Fetch API, window.postMessage() method and a lot of streaming APIs using typed arrays.
  A word about performance

  Array Object construction is being done inside JavaScript VM and Array Objects are being allocated inside VM's heap, while typed arrays allocated outside and involving the browser bindings.
  Sequential reads/writes to both types of arrays are nearly equally fast, while Array Object will be slower for out-of-the-bounds writes (as it will require reallocation).
  Conclusions here:
  
       
  • Typed Arrays are beneficial when you need long-sustained array with specific size and type, and for use cases mentioned above   
  • Array Objects are beneficial when you need dynamic array or when you need temporary array (because of allocation method)  

友荐云推荐




上一篇:Weex学习与实践(三):iOS原理篇
下一篇:tesseract.js - 从图片里抓取文字
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

StevieMill 发表于 2016-10-17 22:35:55
在神经的人群里呆久了,我发现我正常了。
回复 支持 反对

使用道具 举报

nbauto 发表于 2016-11-19 21:04:48
夏天就是不好,穷的时候我连西北风都没得喝……
回复 支持 反对

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

© 2001-2016 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表