Javascript 如何将Int32存储到ArrayBuffer中

Javascript 如何将Int32存储到ArrayBuffer中,javascript,node.js,Javascript,Node.js,我有一个ArrayBuffer,它的长度可能会有所不同,这取决于它的内容,因此我需要从数组中的不同索引连续读取和写入不同的数据类型。有没有一种方法可以做到这一点,而不必每次构造一个新的类型化数组 我已经尝试通过为每个值构造新的类型化数组来读取和写入此ArrayBuffer,但是分析我的代码会发现使用这种方法会带来很多开销 const myArray = new ArrayBuffer(0x800) // populated from elsewhere const readInt32 = (ar

我有一个ArrayBuffer,它的长度可能会有所不同,这取决于它的内容,因此我需要从数组中的不同索引连续读取和写入不同的数据类型。有没有一种方法可以做到这一点,而不必每次构造一个新的类型化数组

我已经尝试通过为每个值构造新的类型化数组来读取和写入此ArrayBuffer,但是分析我的代码会发现使用这种方法会带来很多开销

const myArray = new ArrayBuffer(0x800) // populated from elsewhere
const readInt32 = (array, index) => {
  const typedArray = new Uint32Array(array, index, 1)
  return typedArray[0]
}
const writeInt32 = (array, index, value) => {
  const typedArray = new Uint32Array(array, index, 1)
  typedArray[0] = value
}

const sourceAddress = readInt32(myArray, 0)
const targetAddress = readInt32(myArray, 4)
const value = readInt32(myArray, sourceAddress)
writeInt32(myArray, targetAddress, value + 1)
从技术上讲,这是可行的,但对于我正在进行的阅读和写作来说,这会带来太多的开销。有没有更好的方法来实现
readInt32
writeInt32