JavaScript 类型数组
JavaScript 类型数组新功能?
类型数组类型
2025 年 7 月修订
| 名称 | 范围 | 类型 |
|---|---|---|
| Int8Array | -128 / 127 | 1 字节有符号整数 |
| Uint8Array | 0 / 255 | 1 字节无符号整数 |
| Uint8ClampedArray | 0 / 255 | 1 字节无符号整数 |
| Int16Array | -32768 / 32767 | 2 字节有符号短整型 |
| Uint16Array | 0 / 65535 | 2 字节无符号短整型 |
| Int32Array | -231 / 231 - 1 | 4 字节有符号长整型 |
| Uint32Array | 0 / 232 - 1 | 4 字节无符号长整型 |
| BigInt64Array | -263 / 263 - 1 | 8 字节有符号大整数 |
| BigUint64Array | 0 / 264 - 1 | 8 字节无符号大整数 |
| Float16Array | -65504 / 65504 | 2 字节浮点数 |
| Float32Array | -3.4e38 / 3.4e38 | 4 字节浮点数 |
| Float64Array | -1.8e308 / 1.8e308 | 8 字节浮点数 |
类型化数组的优势
类型化数组提供了一种处理二进制数据的方法,其效率堪比 C 语言中的数组。
类型化数组直接使用原始内存,因此 JavaScript 可以直接将其传递给任何函数,而无需将数据转换为其他表示形式。
对于需要使用原始二进制数据的函数(例如电脑游戏、WebGL、Canvas、文件 API 和媒体 API),类型化数组比普通数组速度快得多。
支持类型化数组的浏览器 API
获取 API 示例
fetch(url)
.then(request => request.arrayBuffer())
.then(arrayBuffer =>...);画布示例
const canvas = document.getElementById('my_canvas');
const context = canvas.getContext('2d');
const imageData = context.getImageData(0, 0, canvas.width, canvas.height);
const uint8ClampedArray = imageData.data;浏览器支持
类型化数组 是 ECMAScript 6 (ES6 2015) 的一项特性。
JavaScript 2015 自 2017 年 6 月起,所有浏览器均已支持 JavaScript 2015。
| Chrome 51 |
Edge 15 |
Firefox 54 |
Safari 10 |
Opera 38 |
| 2016年5月 | 2017年4月 | 2017年6月 | 2016年9月 | 2016年6月 |
