Replace buffer implementation with standard Uint8Array
See original GitHub issueThe conditional use of node’s Buffer versus a shim based on Uint8Array leads to subtle incompatibilities between node and the browser. I just spent a couple hours debugging an issue in mapbox-gl-js that stems from the following difference:
var Pbf = require('pbf');
var pbf = new Pbf();
pbf.writeVarint(0);
console.log(pbf.finish().buffer.byteLength);
In the browser, this prints 16, as expected. In node, it prints 8192.
To eliminate this gotcha, pbf should use standard Uint8Array’s everywhere, and drop usage of Buffer.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Uint8Array - JavaScript - MDN Web Docs
Chrome Edge
Uint8Array Full support. Chrome7. Toggle history Full support. Edge12...
Uint8Array() constructor Full support. Chrome7. Toggle history Full support. Edge12...
Constructor without parameters Full support....
Read more >Buffer | Node.js v19.3.0 Documentation
The Buffer class is a subclass of JavaScript's Uint8Array class and extends it with methods that cover additional use cases. Node.js APIs accept...
Read more >ArrayBuffer, binary arrays - The Modern JavaScript Tutorial
ArrayBuffer, binary arrays · Uint8Array – treats each byte in ArrayBuffer as a separate number, with possible values from 0 to 255 (a...
Read more >Uint8Array to string in Javascript - Stack Overflow
We're just extracting the ArrayBuffer from the Uint8Array and then converting that to a proper NodeJS Buffer. Then we convert the Buffer to...
Read more >How to work with buffers in Duktape 2.x
ES2015, DUK_TAG_OBJECT, [object ArrayBuffer], Standard object type for ... In Node.js v6.7.0+ a Buffer is implemented as an Uint8Array with a custom ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I still think we should just use and accept the performance of
Uint8Array. High-performance node applications are going to want to use a native module for pbfs anyway.The current code forces copying the data in the browser when you do
new Pbf(uint8Array).Working on this. Managed to get the Uint8Array decoding perf drop to ~9% from ~46%.