请选择 进入手机版 | 继续访问电脑版

技术控

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

[其他] Exploring JavaScript: Typed Arrays

[复制链接]
森末 发表于 2016-10-17 10:47:37
207 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

Exploring JavaScript: Typed Arrays
    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:
  1. var arr = new Uint16Array(10);
复制代码
  1. arr[0] = 0xFFFF;
复制代码
  1. console.log(arr[0]);
复制代码
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:
  1. var arr = new Uint16Array(10);
复制代码
  1. arr[0] = 0xFFFF;
复制代码
  1. console.log(~arr[0]); // -65535
复制代码
ArrayBuffer & DataView

  ArrayBuffer & DataView are parts of Typed Array implementation.
   ArrayBufferis basically holding the data of Typed Array.
  Per example:
  1. var fileArrayBuffer = reader.readAsArrayBuffer(file);
复制代码
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:
  1. var arr = new Uint16Array(fileArrayBuffer);
复制代码
Now we can access this data in a regular way:
  1. String.fromCharCode(
复制代码
  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:
  1. struct data { unsigned int id; char[10] username;}
复制代码
We can read it from js by doing something like this:
  1. arr[0] = 0xFFFF;0
复制代码
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:
  1. arr[0] = 0xFFFF;1
复制代码
Canvas

  Yes, canvas image data is typed array:
  1. arr[0] = 0xFFFF;2
复制代码
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:
  1. arr[0] = 0xFFFF;3
复制代码
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-2017 Comsenz Inc. Design: Dean. DiscuzFans.

返回顶部 返回列表