当前位置:网站首页>This article explains the concepts of typed array, arraybuffer, typedarray, DataView, etc
This article explains the concepts of typed array, arraybuffer, typedarray, DataView, etc
2022-06-30 13:30:00 【Front end life of Lvan】
Why do I need typed arrays
JavaScript A typed array is an array like object , It also provides a mechanism for accessing raw binary data .
js Processing images 、 Audio and video editing , Usually you need to process a large amount of the same type of data , For example, the pixel data of a picture , Coordinate data, etc . Instead, use ordinary js Array , The number of stored objects can increase and decrease dynamically , And can store any JavaScript value , There may be performance problems . therefore js Typed arrays are introduced .
The creation of a typed array should specify the data type and length , So it will be much more efficient , And it doesn't support push pop Wait for the operation .
Composition of typed arrays
The type array is split into two parts :
- buffer Buffer
- View View
Buffer by ArrayBuffer Object implementation , It describes a data block , And can't access .
If you want to access , The view is needed .
A bit like the concept of classes and objects .
buffer Buffer
Buffer by ArrayBuffer Object implementation , That is, the part framed in the above figure ,ArrayBuffer Object represents the original binary data , So how to operate the cache part ? There are two ways :
- Type array view
- Data view DataView
View View
Just to summarize ,TypedArray Views are used to read and write binary data of simple types ,DataView Views are used to read and write binary data of complex types .
Type array view TypedArray
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Int8Array
| data type | bytes | meaning | Corresponding C Language type |
|---|---|---|---|
| Int8 | 1 | 8 Bit signed integer | signed char |
| Uint8 | 1 | 8 Bit unsigned integer | unsigned char |
| Uint8Clamped | 1 | 8 Bit unsigned integer ( Auto filter overflow ) | unsigned char |
| Int16 | 2 | 16 Bit signed integer | short |
| Uint16 | 2 | 16 Bit unsigned integer | unsigned short |
| Int32 | 4 | 32 Bit signed integer | int |
| Uint32 | 4 | 32 Bit unsigned integer | unsigned int |
| Float32 | 4 | 32 Bit floating point | float |
| Float64 | 8 | 64 Bit floating point | double |
grammar :
new Int8Array(length);
new Int8Array(typedArray);
new Int8Array(object);
new Int8Array(buffer [, byteOffset [, length]]);
give an example :
// Construct objects with length parameters
var int8 = new Int8Array(2);
int8[0] = 42;
console.log(int8[0]); // 42
console.log(int8.length); // 2
console.log(int8.BYTES_PER_ELEMENT); // 1
// Construct an object as an array
var arr = new Int8Array([21,31]);
console.log(arr[1]); // 31
// Construct objects from another array
var x = new Int8Array([21, 31]);
var y = new Int8Array(x);
console.log(y[0]); // 21
// from ArrayBuffer Construction object
var buffer = new ArrayBuffer(8);
var z = new Int8Array(buffer, 1, 4);
Data view DataView
grammar :
new DataView(buffer [, byteOffset [, byteLength]])
byteOffset: this DataView The first byte of the object is in buffer Byte offset in . If not specified , By default, it starts from the first byte .
byteLength: this DataView Object's byte length . If not specified , The length of this view will match buffer The length of .
// create an ArrayBuffer with a size in bytes
const buffer = new ArrayBuffer(16);
// Create a couple of views
const view1 = new DataView(buffer);
const view2 = new DataView(buffer, 12, 4); //from byte 12 for the next 4 bytes
view1.setInt8(12, 42); // put 42 in slot 12
console.log(view1.getInt8(12));
console.log(view2.getInt8(0));
// expected output: 42
There are many methods provided as follows :
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/DataView
example
Use canvas After drawing the picture , You can get every pixel of the picture rgba The data of , This data is stored in the buffer , It can be used Uint8Array To get , To be specific, see js: Use canvas Check the brightness of the picture
Reference documents
https://www.jianshu.com/p/40d4a4d133e8
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Typed_arrays
边栏推荐
- 正则系列之断言Assertions
- MySQL access denied, opened as Administrator
- App wechat payment unicloud version of uniapp payment (with source code)
- The independent station is Web3.0. The national "14th five year plan" requires enterprises to build digital websites!
- Introduction to the renewal of substrate source code: the pledge amount is greatly reduced, and rocksdb can be completely disabled
- navicat数据库建表是没有utf8选项。
- Development of unity script program
- Clearing TinyMCE rich text cache in elementui
- Data Lake (11): Iceberg table data organization and query
- Goods and services - platform properties
猜你喜欢

Resource realization applet opening wechat official small store tutorial

A keepalived high availability accident made me learn it again!

【招聘(广州)】成功易(广州).Net Core中高级开发工程师

Kaniko official documents - build images in kubernetes
![[kali] Kali system, software update (with image source)](/img/ac/43a3f81d50ab6866271b500b142252.png)
[kali] Kali system, software update (with image source)

【刷题篇】供暖器
![[Select] resource realization information, news, we media, blog applet (can be drained, open traffic master, with PC background management)](/img/e7/1c34d8aa364b944688ec2ffb4feb7c.jpg)
[Select] resource realization information, news, we media, blog applet (can be drained, open traffic master, with PC background management)

资源变现小程序开通微信官方小商店教程

rxjs Observable 两大类操作符简介

我如何才能保护我的私钥?
随机推荐
Tronapi- wavefield interface - source code without encryption - can be opened in two places - interface document attached - encapsulation based on thinkphp5 - detailed guidance of the author - 21:59:3
Illustration creating a stored procedure using Navicat for MySQL
Read all the knowledge points about enterprise im in one article
【C】 In depth understanding of pointers and callback functions (Introduction to simulating qsort)
Dark horse notes -- List series collections and generics
嵌入式开发:5个可能不再被禁止的C特征
Postman automatically generates curl code snippets
Resource realization applet opening traffic main tutorial
损失函数:DIOU loss手写实现
Introduction to two types of rxjs observable operators
Hangzhou E-Commerce Research Institute: the official website (website) is the only form of private domain
Common UI components
Golang Basics - string and int, Int64 inter conversion
mysql拒绝访问、管理员身份打开的
ERROR: Cannot uninstall ‘PyYAML‘. It is a distutils installed project and thus we cannot accurately
WTM major updates, multi tenancy and single sign on
IDEA 2021.3 执行 golang 报错:RNING: undefined behavior version of Delve is too old for Go version 1.18
你想要的异常知识点都在这里了
Js根据相同值将数组转换为二维数组
Unity 频繁切换分支 结果模型出现莫名其妙的错误