binary-extract
Advanced tools
Comparing version
@@ -19,2 +19,3 @@ | ||
var mark = code('"'); | ||
var backslash = code('\\'); | ||
@@ -39,2 +40,8 @@ /** | ||
c = buf[i]; | ||
if (c == backslash) { | ||
i++; | ||
continue; | ||
} | ||
if (c == mark) { | ||
@@ -151,5 +158,4 @@ inString = !inString; | ||
var json = buf.toString('utf8', start, end); | ||
if (json[0] == '"') return json.slice(1, json.length - 1); | ||
return JSON.parse(json); | ||
} | ||
{ | ||
"name": "binary-extract", | ||
"description": "Extract a value from a binary json blob", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"repository": "segmentio/binary-extract", | ||
@@ -16,2 +16,2 @@ "license": "MIT", | ||
} | ||
} | ||
} |
@@ -6,2 +6,4 @@ | ||
[](http://travis-ci.org/segmentio/binary-extract) | ||
## Example | ||
@@ -27,4 +29,8 @@ | ||
With the object from `bench.js`, `extract()` is ~2-4x faster than | ||
`JSON.parse(buf.toString())`. | ||
`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 | ||
@@ -31,0 +37,0 @@ |
@@ -19,3 +19,3 @@ | ||
}) | ||
it('should ignore too nested values', function(){ | ||
it('should ignore too deeply nested values', function(){ | ||
var buf = toBuf({ foo: { beep: 'boop', bar: 'oops' }, bar: 'baz' }); | ||
@@ -38,2 +38,8 @@ equal(extract(buf, 'bar'), 'baz'); | ||
}) | ||
it('should escape with backslash', function(){ | ||
var buf = toBuf({ beep: '\"', foo: 'bar' }); | ||
equal(extract(buf, 'foo'), 'bar'); | ||
var buf = toBuf({ foo: 'bar\"baz' }); | ||
equal(extract(buf, 'foo'), 'bar\"baz'); | ||
}); | ||
}) | ||
@@ -40,0 +46,0 @@ |
6473
13.09%9
28.57%206
5.1%52
13.04%