Socket
Socket
Sign inDemoInstall

str2buf

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    str2buf

isomorphic string <=> buffer in 215 bytes


Version published
Weekly downloads
62K
decreased by-11.83%
Maintainers
1
Install size
9.98 kB
Created
Weekly downloads
 

Readme

Source

str2buf NPM Build

isomorphic string <=> buffer in 215 bytes

Install

npm install str2buf

Usage

You must provide strings where each character is represented by a single byte (also known as the binary string format). For example, the output of a atob operation or any ASCII string would be allowed.

const str2buf = require('str2buf')

str2buf.toUint8Array('hello')
// new Uint8Array([104,101,108,108,111])

str2buf.fromUint8Array(new Uint8Array([104,101,108,108,111]))
// 'hello'

str2buf.toBuffer('hello')
// ArrayBuffer

str2buf.fromBuffer(new Uint8Array([104,101,108,108,111]).buffer)
// 'hello'

Can it be smaller?

If you use ES6 imports with a bundler that supports tree-shaking, yes!

import { fromUint8Array } from 'str2buf'

I wanna go fast!

The Node implementation is just a proxy to Node's Buffer object to be as fast as possible. The default browser implementation optimizes for size, so if you're looking for raw speed, import like this:

import str2buf from 'str2buf/fast/str2buf.mjs'
Perf profile
⏱ browser performance on 1000 arrays or strings of size 25003:
str2buf.fromUint8Array: 834.135ms
str2buf.toUint8Array: 771.099ms
str2buf.fromBuffer: 785.060ms
str2buf.toBuffer: 590.003ms

⏱ fast browser performance on 1000 arrays or strings of size 25003:
str2buf.fromUint8Array: 245.842ms
str2buf.toUint8Array: 165.240ms
str2buf.fromBuffer: 234.920ms
str2buf.toBuffer: 160.021ms

⏱ node performance on 1000 arrays or strings of size 25003:
str2buf.fromUint8Array: 20.213ms
str2buf.toUint8Array: 15.655ms
str2buf.fromBuffer: 32.880ms
str2buf.toBuffer: 21.514ms

License

MIT

Keywords

FAQs

Last updated on 07 Jan 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc