Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/juliangruber/binary-extract
Extract a value from a buffer of json without parsing the whole thing
https://github.com/juliangruber/binary-extract
Last synced: 8 days ago
JSON representation
Extract a value from a buffer of json without parsing the whole thing
- Host: GitHub
- URL: https://github.com/juliangruber/binary-extract
- Owner: juliangruber
- Created: 2014-03-17T12:39:00.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2018-02-10T07:44:07.000Z (over 6 years ago)
- Last Synced: 2024-05-21T12:11:00.264Z (6 months ago)
- Language: JavaScript
- Size: 23.4 KB
- Stars: 153
- Watchers: 7
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: History.md
Awesome Lists containing this project
- awesome-nodejs-cn - binary-extract - 无需解析整个对象就可以从 JSON 缓冲区中提取一个值的工具 (包 / 解析)
- awesome-nodejs-cn - binary-extract - **star:153** 从JSON缓冲区中提取一个值,而不需要解析整个内容 (包 / 解析)
- awesome-nodejs - binary-extract - Extract a value from a buffer of JSON without parsing the whole thing. (Packages / Parsing)
- awesome-nodejs - binary-extract - Extract a value from a buffer of json without parsing the whole thing - ★ 139 (Parsing)
- awesome-node - binary-extract - Extract a value from a buffer of JSON without parsing the whole thing. (Packages / Parsing)
- awesome-nodejs-cn - binary-extract - 从JSON缓冲区中提取一个值而无需解析整个对象的工具. (目录 / 解析工具)
- awesome-nodejs-pure-js - binary-extract
README
# binary-extract
Extract one or more values from a buffer of json without parsing the whole thing.
[![build status](https://secure.travis-ci.org/juliangruber/binary-extract.png)](http://travis-ci.org/juliangruber/binary-extract)
## Example
```js
var extract = require('binary-extract');var buf = new Buffer(JSON.stringify({
foo: 'bar',
bar: 'baz',
nested: {
bar: 'nope'
}
}));var value = extract(buf, 'bar');
// => 'baz'var values = extract(buf, ['foo', 'nested'])
// => ["bar", {"bar":"nope"}]
```## Perf
With the object from `bench.js`, `extract()` is ~2-4x faster than
`JSON.parse(buf.toString())`. It is also way more memory efficient as the
blob stays out of the V8 heap.The big perf gain comes mainly from not parsing everything and not
converting the buffer to a string.## Installation
```bash
$ npm install binary-extract
```## API
### extract(buf, keys)
Extract the value of `keys` in the json `buf`.
The value can be any valid JSON structure.
If `keys` is a __String__, returns a value. If `keys` is an __Array__ of
keys, returns an array of values.## Sponsors
This module is proudly supported by my [Sponsors](https://github.com/juliangruber/sponsors)!
Do you want to support modules like this to improve their quality, stability and weigh in on new features? Then please consider donating to my [Patreon](https://www.patreon.com/juliangruber). Not sure how much of my modules you're using? Try [feross/thanks](https://github.com/feross/thanks)!
## License
MIT