The JS runtimes
Published in

The JS runtimes

Buffer.from: Deno’s equivalent of Node.js

This is a part of the series: Deno’s equivalent of Node.js. In the brief articles that are part of this series, we explore the Deno equivalent of commonly used functions in Node.js. The articles are written in no particular order.

In this article, we’ll see the Deno’s equivalent of Node.js’s Buffer.from.

Buffer.from

Node.js’s Buffer.from is used to create a buffer from a variety of sources like:

  • Array
  • Buffer
  • String
  • Hex string

Let’s see how to do the same in Deno.

Except for array, there is no direct equivalent in Deno (not yet)

Node’s Buffer is part of the core module, while Deno’s Buffer is in both places (but will be out of the core runtime very soon). To use in the recommended way, import Deno’s Buffer from the standard library’s io module:

  • In Node.js, an array containing bytes can be directly used
  • In Deno, an array containing bytes can be directly used
  • In Node.js, a buffer can be used directly
  • In Deno, readFrom is required to use input Buffer as a Reader
  • In Node.js, a string can be directly given to the Buffer
  • In Deno, a string needs to be encoded before giving to the Buffer

A hex string is an ASCII string contains bytes in hex format. For example —

  • In Node.js, hex string can be used with encoding type as ‘hex’
  • In Deno, hex string needs to be converted to bytes before giving to Buffer

This story is a part of the exclusive medium publication on Deno: Deno World.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store