未加星标

Exploring JavaScript: Typed Arrays

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

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

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:

var arr = new Uint16Array(10); arr[0] = 0xFFFF; 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:

var arr = new Uint16Array(10); arr[0] = 0xFFFF; 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:

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:

var arr = new Uint16Array(fileArrayBuffer);

Now we can access this data in a regular way:

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:

struct data {
unsigned int id;
char[10] username;
}

We can read it from js by doing something like this:

var buf = new ArrayBuffer(11);
var id = new Uint8Array(buf, 0, 1);
var username = new Uint8Array(buf, 1, 10);

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:

gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoordinates), gl.STATIC_DRAW); Canvas

Yes, canvas image data is typed array:

var uint8ClampedArray = ctx.getImageData(...).data;

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:

webSocket.binaryType = 'arraybuffer'; 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)

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: JavaScriptJavaWebGLXMLECMAScriptWebSocketUFUTTIFirefox
分页:12
转载请注明
本文标题:Exploring JavaScript: Typed Arrays
本站链接:http://www.codesec.net/view/483613.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(16)