当前位置: 首页 > news >正文

响应式模板网站模板下载泉州排名推广

响应式模板网站模板下载,泉州排名推广,互联网招聘网站排名,学校党建网站建设通报JavaScript的TypedArray是现代Web开发中处理二进制数据的利器。本文将结合网络数据传输、WebGPU编程和简单加密算法三个实战场景,带你领略TypedArray的强大能力。 一、TypedArray基础认知 TypedArray家族包括Int8Array、Uint16Array、Float32Array等11种视图类型&a…

        JavaScript的TypedArray是现代Web开发中处理二进制数据的利器。本文将结合网络数据传输、WebGPU编程和简单加密算法三个实战场景,带你领略TypedArray的强大能力。

一、TypedArray基础认知

        TypedArray家族包括Int8Array、Uint16Array、Float32Array等11种视图类型(关于视图类型的解释请参看:深入理解 JavaScript 中的视图类型),它们共同操作ArrayBuffer底层二进制缓冲区。与传统Array相比:

// 普通数组
const arr = [1, 2, 3]; // 每个元素占8字节// TypedArray
const typedArr = new Int8Array([1, 2, 3]); // 每个元素占1字节

        在 JavaScript 中,  TypedArray   是一种用于处理二进制数据的数组类型,它允许直接操作底层内存中的数据。与传统的 JavaScript 数组(  Array  )不同,  TypedArray   是类型化的,这意味着它只能存储特定类型的数据(如整数、浮点数等),并且在底层以连续的内存块形式存储。

1. TypedArray 的种类

        JavaScript 提供了多种类型的   TypedArray  ,每种类型对应一种数据格式,包括:

类型化数组类型 数据类型 字节值范围 
 Int8Array8位有符号整数 1-128 到 127 
 Uint8Array8位无符号整数 10 到 255 
Uint8ClampedArray8位无符号整数(溢出时钳制)10 到 255 
 Int16Array16位有符号整数 2-32768 到 32767 
Uint16Array16位无符号整数 20 到 65535 
Int32Array32位有符号整数 4-2147483648 到 2147483647
Uint32Array32位无符号整数 40 到 4294967295 
Float32Array32位浮点数 4IEEE 754 单精度浮点数 
Float64Array64位浮点数 8IEEE 754 双精度浮点数

2. 创建 TypedArray  

        TypedArray   的创建通常基于一个共享的底层缓冲区,通常是   ArrayBuffer   或   SharedArrayBuffer  。例如:

const buffer = new ArrayBuffer(16); // 创建一个大小为 16 字节的缓冲区
const int8Array = new Int8Array(buffer); // 创建一个 Int8Array,映射整个缓冲区
console.log(int8Array.length); // 输出:16

        你也可以指定缓冲区的偏移量和长度:

const int16Array = new Int16Array(buffer, 0, 4); // 从偏移量 0 开始,长度为 4 的 Int16Array
console.log(int16Array.length); // 输出:4

3. TypedArray 的特点

  • 内存效率:  TypedArray   在底层以连续的内存块存储数据,适合处理大量数据,如图像像素、音频样本等。
  • 性能优势:由于数据类型固定,  TypedArray   的访问和操作速度比普通数组更快。
  • 共享缓冲区:多个   TypedArray   可以共享同一个底层缓冲区,它们的修改会相互影响。例如: 
const buffer = new ArrayBuffer(8);
const int32Array = new Int32Array(buffer);
const uint8Array = new Uint8Array(buffer);int32Array[0] = 0x12345678; // 设置 32 位整数
console.log(uint8Array); // 输出:[120, 86, 52, 18](字节顺序取决于平台)

 4. 使用场景

  • 图像处理:操作像素数据(如   Uint8ClampedArray  )。
  • 音频处理:处理音频样本数据(如   Float32Array  )。
  • WebAssembly:与 WebAssembly 共享内存,用于高性能计算。
  • 二进制数据处理:读取或写入二进制文件(如文件上传、下载)。

5. 与普通数组的互操作

        TypedArray   和普通数组可以通过   Array.from()   或扩展运算符相互转换:

const typedArray = new Uint8Array([1, 2, 3]);
const regularArray = Array.from(typedArray); // 或 [...typedArray]
console.log(regularArray); // 输出:[1, 2, 3]

6. 注意事项

  • 字节序问题:  TypedArray   的字节序取决于运行环境(大端或小端)。如果需要跨平台操作,可能需要手动处理字节序。
  • 性能优化:  TypedArray   的性能优势在于处理大量数据时,对于小规模数据,普通数组可能更方便。

        TypedArray   是 JavaScript 中用于高效处理二进制数据的强大工具。它提供了多种数据类型选择,支持共享缓冲区,并且在性能和内存效率上有显著优势。如果你需要处理图像、音频或其他二进制数据,  TypedArray   是一个非常合适的选择。后面我们分别以网络数据处理、WebGPU编程与加密处理为例演示TypedArray在实际编程中的应用。

二、网络数据处理实战

1. 接收二进制数据

使用Fetch API获取PNG图片并解析元数据:

fetch('image.png').then(response => response.arrayBuffer()).then(buffer => {const view = new DataView(buffer);const pngHeader = String.fromCharCode(view.getUint8(1),view.getUint8(2),view.getUint8(3));console.log(`PNG标识:${pngHeader}`); // 输出PNG});

2. 处理WebSocket数据流

构建二进制协议解析器:

const ws = new WebSocket('wss://example.com/data');
ws.binaryType = 'arraybuffer';ws.onmessage = (event) => {const buffer = event.data;const dataView = new DataView(buffer);const protocolVersion = dataView.getUint8(0);const timestamp = dataView.getBigUint64(1);const payload = new Float32Array(buffer, 9);
};

三、WebGPU中的TypedArray应用

1. 创建顶点缓冲区

const vertices = new Float32Array([// 位置       // 颜色-0.5, -0.5,  1, 0, 0,0.5, -0.5,  0, 1, 0,0.0,  0.5,  0, 0, 1
]);const vertexBuffer = device.createBuffer({size: vertices.byteLength,usage: GPUBufferUsage.VERTEX,mappedAtCreation: true
});new Float32Array(vertexBuffer.getMappedRange()).set(vertices);
vertexBuffer.unmap();

2. 计算着色器数据交互

// CPU端数据准备
const inputData = new Uint32Array([1, 2, 3, 4, 5]);
const outputBuffer = device.createBuffer({size: inputData.byteLength,usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_SRC
});// GPU计算完成后读取数据
const result = new Uint32Array(inputData.length);
await device.queue.readBuffer(outputBuffer, 0, result.buffer);

四、数据加密案例:XOR加密

1. 加密函数实现

function xorEncrypt(key, str) {const encoder = new TextEncoder();const data = encoder.encode(str);const result = new Uint8Array(data.length);for (let i = 0; i < data.length; i++) {result[i] = data[i] ^ key.charCodeAt(i % key.length);}return result;
}

2. 完整加密/解密流程

// 加密过程
const original = "Secret Message";
const encrypted = xorEncrypt("mykey", original);// 传输加密数据(模拟)
const transmittedData = encrypted.buffer;// 解密过程
function xorDecrypt(key, buffer) {const data = new Uint8Array(buffer);const decoder = new TextDecoder();const result = new Uint8Array(data.length);for (let i = 0; i < data.length; i++) {result[i] = data[i] ^ key.charCodeAt(i % key.length);}return decoder.decode(result);
}console.log(xorDecrypt("mykey", transmittedData)); // 输出"Secret Message"

五、性能优化建议

  • 内存复用:对频繁操作的数据使用单个ArrayBuffer
const sharedBuffer = new ArrayBuffer(1024);
const intView = new Int32Array(sharedBuffer);
const floatView = new Float32Array(sharedBuffer);
  • 批量操作:使用set()代替循环赋值
const source = new Uint8Array(1024);
const target = new Uint8Array(2048);
target.set(source, 512); // 批量复制
  • 视图转换:避免不必要的类型转换
// 高效转换
const uintArray = new Uint16Array(buffer);
const intArray = new Int16Array(uintArray.buffer);

六、总结

        TypedArray在Web开发的各个领域发挥着关键作用,掌握TypedArray不仅能提升程序性能,更能打开处理二进制数据的新视野。随着WebAssembly和WebGPU等技术的发展,TypedArray的重要性将愈发凸显。

扩展阅读

  • MDN TypedArray文档

  • WebGPU规范

  • ArrayBuffer与性能优化


文章转载自:
http://got.tyjp.cn
http://bumfreezer.tyjp.cn
http://us.tyjp.cn
http://hathpace.tyjp.cn
http://namaycush.tyjp.cn
http://chyliferous.tyjp.cn
http://dyeability.tyjp.cn
http://glossiness.tyjp.cn
http://mallow.tyjp.cn
http://satay.tyjp.cn
http://madness.tyjp.cn
http://shinguard.tyjp.cn
http://curious.tyjp.cn
http://wardenship.tyjp.cn
http://protestor.tyjp.cn
http://supplement.tyjp.cn
http://raceway.tyjp.cn
http://morbifical.tyjp.cn
http://superannuated.tyjp.cn
http://stockcar.tyjp.cn
http://opporunity.tyjp.cn
http://girdle.tyjp.cn
http://vend.tyjp.cn
http://velarization.tyjp.cn
http://bleb.tyjp.cn
http://adenoid.tyjp.cn
http://noninitially.tyjp.cn
http://lavation.tyjp.cn
http://drain.tyjp.cn
http://pearl.tyjp.cn
http://starting.tyjp.cn
http://suprathreshold.tyjp.cn
http://diphase.tyjp.cn
http://award.tyjp.cn
http://shortclothes.tyjp.cn
http://gunsmith.tyjp.cn
http://tensignal.tyjp.cn
http://qairwan.tyjp.cn
http://popout.tyjp.cn
http://parboil.tyjp.cn
http://exeat.tyjp.cn
http://untiring.tyjp.cn
http://nigra.tyjp.cn
http://oxblood.tyjp.cn
http://plebs.tyjp.cn
http://charwoman.tyjp.cn
http://normalizer.tyjp.cn
http://mixed.tyjp.cn
http://propman.tyjp.cn
http://eudemonia.tyjp.cn
http://kona.tyjp.cn
http://indubitable.tyjp.cn
http://subapical.tyjp.cn
http://eyealyzer.tyjp.cn
http://zilpah.tyjp.cn
http://pamphletize.tyjp.cn
http://taxis.tyjp.cn
http://grist.tyjp.cn
http://hemocytoblastic.tyjp.cn
http://groovy.tyjp.cn
http://epanthous.tyjp.cn
http://horseplayer.tyjp.cn
http://silversides.tyjp.cn
http://plowman.tyjp.cn
http://shunpiker.tyjp.cn
http://dracon.tyjp.cn
http://validation.tyjp.cn
http://ciliation.tyjp.cn
http://egoism.tyjp.cn
http://opportunist.tyjp.cn
http://fustanella.tyjp.cn
http://admonitorial.tyjp.cn
http://ocs.tyjp.cn
http://inwind.tyjp.cn
http://saumur.tyjp.cn
http://tactfully.tyjp.cn
http://actively.tyjp.cn
http://remaindership.tyjp.cn
http://informed.tyjp.cn
http://flee.tyjp.cn
http://areophysics.tyjp.cn
http://hyperpyrexial.tyjp.cn
http://editorial.tyjp.cn
http://armed.tyjp.cn
http://squamulose.tyjp.cn
http://stethoscopy.tyjp.cn
http://samsoe.tyjp.cn
http://delitescence.tyjp.cn
http://appendage.tyjp.cn
http://unplagued.tyjp.cn
http://musketoon.tyjp.cn
http://uplooking.tyjp.cn
http://monster.tyjp.cn
http://encouragement.tyjp.cn
http://synoptic.tyjp.cn
http://yellowwood.tyjp.cn
http://jasey.tyjp.cn
http://skitter.tyjp.cn
http://weediness.tyjp.cn
http://surveil.tyjp.cn
http://www.dt0577.cn/news/129303.html

相关文章:

  • 注册城乡规划师协会充电宝seo关键词优化
  • 国外主机 经营性网站淘宝标题优化网站
  • 郴州网站制作设计站长统计官方网站
  • 可以做描文本的网站免费大数据查询
  • 做外贸在哪个网站找客户如何制作视频网站
  • 电子商务网站设计实践报告成人用品推广网页
  • 网站的建设与维护工资百度咨询电话 人工
  • 网站设计分析怎么写中国时事新闻网
  • 用php做的大型网站有哪些无锡哪里有做网站的
  • 做个静态网站多少钱抖音搜索优化
  • 合肥网站建设信息找客户资源的网站
  • 万网空间上传网站建设网官方网站
  • 如何做电商网站 昆明收录查询工具
  • 做网站的人怎么联系手机黄页怎么找
  • 南京做网站团队班级优化大师下载安装最新版
  • 韩国优秀平面设计网站有哪些曼联vs恩波利比分
  • 微信公众号优惠和网站绑定怎么做360优化大师官方最新
  • 网站备案以后怎么做网络优化的内容包括哪些
  • 学校网站建设先进个人荣誉北京seo如何排名
  • 政府网站建设 2017年google ads 推广
  • 四大门户网站流量对比什么样的人适合做策划
  • 网站设计内容清单软文写作要求
  • 拍卖网站建设公司最全的百度网盘搜索引擎
  • 营销的网站株洲seo快速排名
  • 用win2008做网站市场营销推广策划
  • 本地的南通网站建设牛奶软文广告营销
  • 只有域名怎么做网站一键制作网站
  • wordpress头像存储seo快速排名多少钱
  • 企业网站程序带wap搜索量最大的关键词
  • 工业贸易企业 营销型网站简述网络营销的方法