将Uint8Array复制到Uint8ClampedArray中似乎比克隆Uint8Array并使用其底层ArrayBuffer慢得多
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。
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)。
发布于 2021-12-02 19:52:42
我在浏览器或NodeJs中看不到这些时间差
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);$ 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.525mshttps://stackoverflow.com/questions/70203564
复制相似问题