https://github.com/kevlened/str2buf
:dash: isomorphic string <=> buffer in 183 bytes
https://github.com/kevlened/str2buf
buffer fast isomorphic javascript string
Last synced: 22 days ago
JSON representation
:dash: isomorphic string <=> buffer in 183 bytes
- Host: GitHub
- URL: https://github.com/kevlened/str2buf
- Owner: kevlened
- License: mit
- Created: 2018-01-06T18:00:15.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-02-08T02:52:43.000Z (over 7 years ago)
- Last Synced: 2025-02-25T20:04:52.650Z (7 months ago)
- Topics: buffer, fast, isomorphic, javascript, string
- Language: JavaScript
- Homepage:
- Size: 27.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# str2buf [](https://npmjs.com/package/str2buf) [](https://travis-ci.org/kevlened/str2buf)
isomorphic string <=> buffer in 198 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.
```javascript
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')
// ArrayBufferstr2buf.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!
```javascript
import { fromUint8Array } from 'str2buf'
```#### Perf profile
```
⏱ 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