What are the benefits of using Uint8List over List <int> when working with byte arrays in Dart?

I am writing a Dart library in which I very regularly deal with byte arrays or byte strings. Since Dart does not have a byte type or an array type, I use List for all byte arrays.

Is this a good practice? I only recently found out about the existence of Uint8Lista package dart:typed_data. It is clear that this class seeks implementation for byte arrays.

But does he have any direct advantages?

I can imagine that he always checks for new elements so that the user can verify that there are no integers in the list without bytes. But are there any other advantages or differences?

There is also a class called ByteArray, but it seems like a pretty inefficient alternative to List ...

+4
source share
2 answers

The advantage should be that Uint8List consumes less memory than a regular list, since it is known from the very beginning that each element size is one byte. Uint8List can also be mapped directly to basic optimized Uint8List types (e.g. in Javascript). Copies of list fragments are also easier to perform, since all bytes are laid out in memory, and therefore a slice can be directly copied in one operation to another type of Uint8List (or equivalent).

, , , Uint8List Dart.

+3

, - 3 - , (smi), () (bint). VM .

smi, , , , . . : smi , .

, , smi . , .

, . , , . , Int8List . smi , .

, , , , .

+2

All Articles