5 ms·
TBH I prefer Buffer as an abstraction. Too bad it didn't make it into native JS standard. I can't think of many use cases in JS land were Uint8Array, Uint16Arra
by jongjong 3y ago
TBH I prefer Buffer as an abstraction. Too bad it didn't make it into native JS standard. I can't think of many use cases in JS land were Uint8Array, Uint16Array, Uint32Array, Int8Array would be absolutely necessary. Seems more useful for perf optimizations. For WebAssembly? Surely a plain array of numbers can be used in most cases. The main use case for Buffer IMO is to convert between different formats like Base64, hex or for sending raw binary data over a transport; ultimately we just need an object to represent binary. It doesn't seem appropriate for a high level language like JS to concern itself with the details of CPU architecture which warrant thinking of binary in terms of 8-bits, 16-bits or 32-bits in the first place. As an abstraction, it's rather arbitrary to treat these numbers as special. It really seems to come down to a marginal performance optimization.
- mofle 3y ago> I can't think of many use cases in JS land were Uint8Array, Uint16Array, Uint32Array, Int8Array would be absolutely necessary. Buffer is a subclass of Uint8Array.
- tomjakubowski 3y agoSized numerics are useful even in high level, loosely and dynamically typed languages. Two examples are binary (de)serialization and OpenGL. In Python, libraries for those generally use sized numerics. High level languages will still want to do low level things
- SuboptimalEng 3y agoTyped arrays are essential for web apps that use WebGL and WebGPU. Being able to send this type of data to run computations on the GPU can give you 1000x speed up. You can see it in action on this WebGL fluid simulator[0] by PavelDoGreat. [0] https://github.com/PavelDoGreat/WebGL-Fluid-Simulation https://github.com/PavelDoGreat/WebGL-Fluid-Simulation
- mateuspires 3y agoThis simulator os awesome.
- ninepoints 3y agoThis isn't an argument against buffer though. Raw memory is raw memory
- iso8859-1 3y ago> For WebAssembly? Surely a plain array of numbers can be used in most cases. So a list of floats? No! Let's not use floats everywhere just because JavaScript does.
- akira2501 3y agoBuffer is a utility. It combines several abstractions that are common in nature into one highly useful class. I quite prefer using it as well. This is what I feel the node people get right over the ES people, the ES ideology is so abstracted and pushes everything out into small utility classes so you have to create a handful of different objects with odd combinations of methods to get one useful conversion done. The ES people also seem to have no love of the CLI or for type types of debugging and testing done there. As a result, I almost always choose the node created abstractions over the ES specified ones.