Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bipf

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bipf - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

test/perf.js

52

index.js

@@ -1,2 +0,2 @@

var varint = require('varint')
var varint = require('varint32')

@@ -18,2 +18,3 @@ var STRING = 0 // 000

var bytes = 0
//sets buffer, and returns length
var encoders = [

@@ -52,4 +53,9 @@ function String (string, buffer, start) {

function Boolean (b, buffer, start) {
buffer[start] = +!!b
return b == null ? 0 : 1
if(b !== null)
buffer[start] = (
b === false ? 0
: b === true ? 1
: 2 //undefined
)
return b === null ? 0 : 1
}

@@ -143,4 +149,9 @@ ]

if(length === 0) return null
if(buffer[start] > 1) throw new Error('invalid boolnull')
return buffer[start] === 1
if(buffer[start] > 2) throw new Error('invalid boolnull')
if(length > 1) throw new Error('invalid boolnull, length must = 1')
return (
buffer[start] === 0 ? false
: buffer[start] === 1 ? true
: undefined
)
}

@@ -162,4 +173,4 @@ ]

return OBJECT
else if('boolean' === typeof value || null === value)
return BOOLNULL
else if('boolean' === typeof value || null == value)
return BOOLNULL //boolean, null, undefined
}

@@ -174,6 +185,10 @@

function encode (value, buffer, start) {
start = start | 0
var type = getType(value)
if('function' !== typeof encodingLengthers[type])
throw new Error('unknown type:'+type+', '+JSON.stringify(value))
var len = encodingLengthers[type](value)
if(!buffer)
throw new Error('buffer must be provided')
buffer = Buffer.allocUnsafe(len)
//throw new Error('buffer must be provided')
if(type === 7) throw new Error('reserved type')

@@ -291,3 +306,11 @@ varint.encode(len << TAG_SIZE | type, buffer, start)

function isNull(tag) {
return tag === 6
}
function isUndefined(tag, firstByte) {
return tag === 0xE && firstByte === 2
}
function compare (buffer1, start1, buffer2, start2) {
//handle null pointers...

@@ -305,2 +328,15 @@ // console.log(start1, start2)

//null, lowest value
if(isNull(tag1))
return isNull(tag2) ? 0 : -1
else if(isNull(tag2))
return 1
//undefined, highest value
if(isUndefined(tag1, buffer1[start1+1]))
return isUndefined(tag2, buffer2[start2+1]) ? 0 : 1
else if(isUndefined(tag2, buffer2[start2+1]))
return -1
//allow comparison of number types. **javascriptism**

@@ -307,0 +343,0 @@ //maybe it's better to just have one number type? how can I make a varint double?

2

package.json
{
"name": "bipf",
"description": "binary in-place format",
"version": "1.1.0",
"version": "1.2.0",
"homepage": "https://github.com/dominictarr/binary",

@@ -6,0 +6,0 @@ "repository": {

@@ -0,1 +1,4 @@

//create a magic js object that parses as you access fields.
//this is way way slower. not recommended.
var binary = require('./')

@@ -2,0 +5,0 @@ var handler = {

@@ -90,15 +90,23 @@ # bipf

The measurement is milliseconds to perform 10k operations.
Code is at the end of `./test.js`
The measurement is run 10k operations, then divide by number of
ms taken, higher number means more faster!
benchmark code is in `./test/perf.js`
```
binary.encode 228
JSON.stringify 38
binary.decode 212
JSON.parse 52
JSON.parse(buffer) 66
JSON.stringify(JSON.parse()) 96
binary.seek(string) 44
binary.seek(buffer) 10
operation, ops/ms
binary.encode 62.61740763932373
JSON.stringify 325.7328990228013
binary.decode 83.40283569641367
JSON.parse 242.13075060532688
JSON.parse(buffer) 198.4126984126984
JSON.stringify(JSON.parse()) 127.55102040816327
binary.seek(string) 500
binary.seek2(encoded) 1219.5121951219512
binary.seek(buffer) 1333.3333333333333
binary.seekPath(encoded) 558.659217877095
binary.seekPath(compiled) 1265.8227848101267
binary.compare() 1785.7142857142858
```
As expected, `binary.encode` is much slower than `JSON.stringify`,

@@ -222,8 +230,16 @@ but it's only 6 times worse.

### seekPath (buffer, start, path, path_start) => pointer
### seekPath (buffer, start, array_of_buffers) => pointer
The same as `seekKey`, except for a recursive path.
`path` should be an array of strings encoded with this format.
`path_start` should a pointer to the encoded array in that buffer.
`path` should be an array of node buffers, just holding the key values,
not encoded as `bipf`.
### createSeekPath(path) => seekPath(buffer, start)
compiles a javascript function that does a seekPath.
this is significantly faster than iterating over a javascript
array and then looking for each thing, because it will get optimized
by the js engine's jit compiler.
## License

@@ -233,1 +249,2 @@

@@ -38,2 +38,15 @@

tape('sort with null undefined', function (t) {
var values = [
null, undefined, 0, -1, 1, 'hello',
Buffer.from('abc'), [], {}, 0.23, true, false
]
var encoded = values.map(encode)
encoded.sort(function (a, b) {
return bipf.compare(a, 0, b, 0)
})
console.log(encoded)
//console.log(encoded.map(function (b) { return bipf.decode(b, 0) }))
t.end()
})

@@ -62,3 +62,4 @@ var binary = require('../')

console.log('encoded:', b.slice(0, l))
var jl = Buffer.byteLength(JSON.stringify(value))
//''+jsonString to get 'undefined' string.
var jl = Buffer.byteLength(''+JSON.stringify(value))
console.log('length:', l, 'json-length:', jl)

@@ -84,2 +85,3 @@ if(l > jl) console.log("WARNING: binary encoding longer than json for:", value)

test(null)
test(undefined) //added undefined for compatibility with charwise
test('')

@@ -95,110 +97,3 @@ test(Buffer.alloc(0))

function encode (string) {
var b = Buffer.alloc(binary.encodingLength(string))
binary.encode(string, b, 0)
return b
}
tape('perf', function (t) {
var value = pkg
var b = Buffer.alloc(binary.encodingLength(value))
var start = Date.now(), json
var json = JSON.stringify(value)
var buffer = new Buffer(JSON.stringify(value))
var N = 100000
console.log('operation, ops/ms')
for(var i = 0; i < N; i++) {
binary.encode(value, b, 0)
}
// ---
start = Date.now()
for(var i = 0; i < N; i++) {
JSON.stringify(value)
}
console.log('JSON.stringify', N/(Date.now() - start))
// ---
start = Date.now()
for(var i = 0; i < N; i++) {
binary.decode(b, 0)
}
console.log('binary.decode', N/(Date.now() - start))
// ---
start = Date.now()
for(var i = 0; i < N; i++) {
JSON.parse(json)
}
console.log('JSON.parse', N/(Date.now() - start))
// ---
start = Date.now()
for(var i = 0; i < N; i++) {
JSON.parse(buffer)
}
console.log('JSON.parse(buffer)', N/(Date.now() - start))
// ---
start = Date.now()
for(var i = 0; i < N; i++) {
JSON.stringify(JSON.parse(json))
}
console.log('JSON.stringify(JSON.parse())', N/(Date.now() - start))
// ---
start = Date.now()
for(var i = 0; i < N; i++) {
binary.decode(b, binary.seekKey(b, binary.seekKey(b, 0, 'dependencies'), 'varint'))
}
console.log('binary.seek(string)', N/(Date.now() - start))
var _varint = encode('varint'), _dependencies = encode('dependencies')
start = Date.now()
for(var i = 0; i < N; i++) {
binary.decode(b, binary.seekKey2(b, binary.seekKey2(b, 0, _dependencies, 0), _varint, 0))
}
console.log('binary.seek2(encoded)', N/(Date.now() - start))
// ---
start = Date.now()
var dependencies = new Buffer('dependencies')
var varint = new Buffer('varint')
for(var i = 0; i < N; i++) {
var c, d
binary.decode(b, d=binary.seekKey(b, c = binary.seekKey(b, 0, dependencies), varint))
}
console.log('binary.seek(buffer)', N/(Date.now() - start))
// ---
start = Date.now()
var path = encode(['dependencies', 'varint'])
for(var i = 0; i < N; i++) {
var c, d
binary.decode(b, d=binary.seekPath(b, c, path))
}
console.log('binary.seekPath(encoded)', N/(Date.now() - start))
// ---
//What Would Mafintosh Do?
//he'd take the path and generate javascript that unrolled seek...
var seekPath = binary.createSeekPath(['dependencies', 'varint'])
start = Date.now()
for(var i = 0; i < N; i++) {
var d
binary.decode(b, d=seekPath(b, 0))
}
console.log('binary.seekPath(compiled)', N/(Date.now() - start))
//compare
var compare = binary.createCompareAt([['name'], ['version']])
start = Date.now()
for(var i = 0; i < N; i++) {
compare(b, 0, b, 0)
}
console.log('binary.compare()', N/(Date.now() - start))
t.end()
})
tape('seekPath', function (t) {

@@ -238,1 +133,3 @@ var path = ['dependencies', 'varint']

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc