Function createDataView

  • Create a DataView from a Uint8Array. This is a convenience function that avoids having to create a DataView manually, which requires passing the byteOffset and byteLength parameters every time.

    Not passing the byteOffset and byteLength parameters can result in unexpected behavior when the Uint8Array is a view of a larger ArrayBuffer, e.g., when using Uint8Array.subarray.

    This function also supports Node.js Buffers.

    Example

    const bytes = new Uint8Array([1, 2, 3]);

    // This is equivalent to:
    // const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
    const dataView = createDataView(bytes);

    Returns

    The DataView.

    Parameters

    • bytes: Uint8Array

      The bytes to create the DataView from.

    Returns DataView