question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Replace buffer implementation with standard Uint8Array

See original GitHub issue

The 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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jfirebaughcommented, Aug 24, 2016

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).

0reactions
mournercommented, Aug 25, 2016

Working on this. Managed to get the Uint8Array decoding perf drop to ~9% from ~46%.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found