javascript - 获取 ArrayBuffer 的视图而不是复制
问题描述
我想做相当于ArrayBuffer.slice
不实际复制内容的操作。用例是将一个非常大 (50mb) 的 ArrayBuffer 转换为字符串
下面你可以看到我正在使用new Uint16Array(buffer, start, chunkSize))
. 这会从 中复制值ArrayBuffer
,这很慢。关于如何提高性能的任何想法?
function arrayBufferToStr(buffer: ArrayBuffer) {
// Use chunks to not go over call stack
// chunks of 1024 bytes * 64
const chunkSize = 1024 * 64
// To right before the last chunk so the last `String.fromCharCode`
// can skip passing a byteLength argument
const endCondition = buffer.byteLength - (chunkSize * 2)
let str = ""
let start = 0
for (start = 0; start < endCondition; start += chunkSize * 2) {
str += String.fromCharCode.apply(null, new Uint16Array(buffer, start, chunkSize))
}
const view = new Uint16Array(buffer, start)
buffer = null
str += String.fromCharCode.apply(null, view)
return str
}
解决方案
推荐阅读
- node.js - 您如何在电子中使用 socket.io?
- laravel - Laravel Sanctum SPA 验证 - 成功登录后未验证
- c# - 在多线程应用程序中创建文件时出现异常 - “文件正在被另一个进程使用”
- r - 如何解决指出 sce 不是数字的 addPerCellQC 错误?
- batch-file - 有没有办法同时运行这两个批处理脚本?在同一个文件上,合并
- java - java中是否有任何公共/全局变量?
- pandas - 如何在熊猫数据框中交换行和列
- javascript - 从钩子问题中为每个元素调用的函数
- git - git push 命令与 master 和 main 冲突
- sql - SQL查询以选择具有改变粒度的记录