The loaders.gl API consistently uses
ArrayBuffers to represent and transport binary data.
One of the design goals of loaders.gl is to provide applications with a single, consistent API that works across (reasonably modern) browsers, worker threads and Node.js. One of the characteristics of this API is how binary data is represented.
loaders.gl "standardizes" on ArrayBuffers for a number of reasons:
- ArrayBuffers are the "canonical" input format for the WebGL API, allowing efficient uploads of large binary data sets to the GPU.
- ArrayBuffers allow ownership to be transferred between threads (Browser Main Thread and WebWorkers), massively improving performance when sending data back from loaders running on web worker to the application/main thread.
- ArrayBuffers are well supported by recent Node.js versions, in fact the traditional Node.js
Bufferclass is now backed by an
ArrayBuffers and Typed Arrays
Recall that typed arrays (e.g.
Float32Array) are just views into array buffers. Every typed array has a
Many loaders.gl functions directly accept typed arrays, which essentially means they accept the associated ArrayBuffer. However, be aware that typed arrays can represent partial views (i.e. they can have offsets) that sometimes need special handling in the application.
Converting between ArrayBuffers and Strings
We use the
Since these classes are central to using ArrayBuffers correctly, loaders.gl provides polyfills for them under Node.js.
Binary data types in JS:
Uint8Arrayand other typed arrays, plus
Examples of "semi-binary" data types in JS:
Array: Array of bytes (elements are numbers between 0 and 255).
String(binary): string in “binary” form, 1 byte per char (2 bytes).
String(base64): string containing the binary data encoded in a base64 form.
Converting between ArrayBuffers and other Binary Formats.