Skip to content Skip to sidebar Skip to footer

Convert Nodejs' Buffer To Browsers' Javascript

I'm converting my code from Node.js to browsers' javascript, but I have a problem with the Buffers in node.js. How can I use them in Javascript? Here's an example: new Buffer('foo'

Solution 1:

With https://github.com/substack/node-browserify you can work with buffers in the Browser by using: https://github.com/toots/buffer-browserify. However: this can be very slow in the browser: For faster access use https://github.com/chrisdickinson/bops


Solution 2:

There is no direct support for Buffer in browser-based JavaScript, and I am not aware of any compatibility library that implements the Buffer API (yet).

The equivalent functionality in the browser is provided by TypedArrays. You can learn about them here:

When porting a Node Buffer-based implementation to browser-based JavaScript, I found these answers helpful:


Solution 3:

To convert back to the string, use the buffer's toString method with a supplied encoding.

http://nodejs.org/docs/latest/api/buffers.html#buffer.toString

var buffer = new Buffer("foo", "utf8");
var str = buffer.toString("utf8");
str === "foo";

Post a Comment for "Convert Nodejs' Buffer To Browsers' Javascript"