首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么将Uint8Array复制到Uint8ClampedArray比将Uint8Array复制到新Uint8Array慢?

为什么将Uint8Array复制到Uint8ClampedArray比将Uint8Array复制到新Uint8Array慢?
EN

Stack Overflow用户
提问于 2021-12-02 16:56:57
回答 1查看 91关注 0票数 1

Uint8Array复制到Uint8ClampedArray中似乎比克隆Uint8Array并使用其底层ArrayBuffer慢得多

代码语言:javascript
复制
const foo = new Uint8Array(0x10000000); // 256MiB
console.time('Copy into Uint8ClampedArray');
const bar = new Uint8ClampedArray(foo);
console.timeEnd('Copy into Uint8ClampedArray');

上面的代码在我的机器(Chrome,MacBook v96 )上的运行时间约为160ms。

代码语言:javascript
复制
const foo = new Uint8Array(0x10000000); // 256MiB
console.time('Clone, then use ArrayBuffer');
const bar = new Uint8ClampedArray(new Uint8Array(foo).buffer);
console.timeEnd('Clone, then use ArrayBuffer');

上面的代码在我的机器上以大约70ms的速度计时。

Firefox给出了类似的统计数据(140ms vs 60ms),而Safari则显示出更极端的差异(550ms vs 30ms)。

EN

回答 1

Stack Overflow用户

发布于 2021-12-02 19:52:42

我在浏览器或NodeJs中看不到这些时间差

代码语言:javascript
复制
foo = new Uint8Array(256 * 1024**2); // 256MiB
console.time(label = 'Copy into Uint8ClampedArray');
bar = new Uint8ClampedArray(foo);
console.timeEnd(label);

foo = new Uint8Array(256 * 1024**2); // 256MiB
console.time(label = 'Clone, then use ArrayBuffer');
bar = new Uint8ClampedArray(new Uint8Array(foo).buffer);
console.timeEnd(label);
代码语言:javascript
复制
$ node -v && node test.js
v17.2.0
Copy into Uint8ClampedArray: 174.049ms
Clone, then use ArrayBuffer: 193.819ms

$ node -v && node test.js
v17.2.0
Copy into Uint8ClampedArray: 170.152ms
Clone, then use ArrayBuffer: 192.41ms

$ node -v && node test.js
v17.2.0
Copy into Uint8ClampedArray: 168.555ms
Clone, then use ArrayBuffer: 209.525ms
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70203564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档