Socket
Socket
Sign inDemoInstall

bencode

Package Overview
Dependencies
Maintainers
2
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bencode - npm Package Compare versions

Comparing version 0.4.3 to 0.5.0

benchmark/decode.js

24

benchmark/buffer-vs-string.js

@@ -1,26 +0,12 @@

var Benchmark = require('benchmark')
var bencode = require('../')
var buf = require('fs').readFileSync(__dirname + '/test.torrent');
var str = buf.toString();
var str = buf.toString('ascii');
var decoding = new Benchmark.Suite({
maxTime: 10
})
.add('bencode buffer', function() {
suite('buffer vs string', function() {
bench('buffer', function() {
bencode.decode(buf);
})
.add('bencode string', function() {
bench('string', function() {
bencode.decode(str);
})
.on( 'cycle', function ( event ) {
console.log( event.target.toString() )
})
.on( 'complete', function ( event, bench ) {
console.log(
'\nFastest is ' + this.filter( 'fastest' ).pluck( 'name' ) + '\n\n'
)
})
.on('error', function() {
console.log(arguments)
})
.run()
})

@@ -0,1 +1,5 @@

## Upcoming
- decode() now throws if it encounters invalid input
## 0.4.3

@@ -2,0 +6,0 @@ * improved performance a lot

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

/**

@@ -49,3 +48,7 @@ * Decodes bencoded data.

return -1
throw new Error(
'Invalid data: Missing delimiter "' +
String.fromCharCode( chr ) + '" [0x' +
chr.toString( 16 ) + ']'
)

@@ -89,7 +92,8 @@ }

var end = decode.find( 0x65 )
var number = decode.data.toString('ascii', decode.position+1, end )
var number = decode.data.toString( 'ascii', decode.position + 1, end )
decode.position += end + 1 - decode.position
return parseInt( number )
return parseInt( number, 10 )
}

@@ -100,14 +104,14 @@

var sep = decode.find( 0x3A )
var length = parseInt(decode.data.toString('ascii', decode.position, sep ), 10)
var length = parseInt( decode.data.toString( 'ascii', decode.position, sep ), 10 )
var end = ++sep + length
decode.position += end - decode.position
decode.position = end
return decode.encoding
? decode.data.toString(decode.encoding, sep, end )
: decode.data.slice( sep, end )
? decode.data.toString( decode.encoding, sep, end )
: decode.data.slice( sep, end )
}
// Expose
// Exports
module.exports = decode

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

/**

@@ -9,6 +8,5 @@ * Encodes data in bencode.

function encode( data ) {
var buffers = []
encode._encode( buffers, data)
return Buffer.concat(buffers)
encode._encode( buffers, data )
return Buffer.concat( buffers )
}

@@ -49,4 +47,9 @@

encode.number = function( buffers, data ) {
buffers.push( new Buffer( 'i' + ( data << 0 ) + 'e' ) )
var maxLo = 4294967295
var hi = ( data / maxLo ) << 0
var lo = ( data % maxLo ) << 0
var val = hi * maxLo + lo
buffers.push(
new Buffer( 'i' + val + 'e'
))
}

@@ -60,5 +63,4 @@

var k
// fix for issue #13 - sorted dicts
var keys = Object.keys(data).sort()
var keys = Object.keys( data ).sort()
var kl = keys.length

@@ -73,3 +75,2 @@

buffers.push( buff_e )
}

@@ -76,0 +77,0 @@

{
"name": "bencode",
"version": "0.4.3",
"version": "0.5.0",
"license": "MIT",

@@ -24,3 +24,3 @@ "description": "Bencode de/encoder",

"devDependencies": {
"benchmark": "",
"matcha": "",
"bencoding": "",

@@ -32,4 +32,6 @@ "dht-bencode": "",

},
"scripts": {
"test": "./node_modules/.bin/mocha"
"test": "node node_modules/mocha/bin/mocha",
"bench": "node node_modules/matcha/bin/matcha benchmark/*.js"
},

@@ -36,0 +38,0 @@

# node-bencode [![build status](https://secure.travis-ci.org/themasch/node-bencode.png)](http://travis-ci.org/themasch/node-bencode)
# node-bencode [![build status](https://secure.travis-ci.org/themasch/node-bencode.png)](http://travis-ci.org/themasch/node-bencode) [![NPM version](https://badge.fury.io/js/bencode.png)](https://npmjs.org/package/bencode)

@@ -41,7 +41,7 @@ A node library for encoding and decoding bencoded data,

```
bencode x 7,939 ops/sec ±2.22% (74 runs sampled)
bencoding x 5,632 ops/sec ±1.47% (86 runs sampled)
dht-bencode x 6,403 ops/sec ±3.45% (87 runs sampled)
bncode x 5,106 ops/sec ±3.63% (82 runs sampled)
dht.js x 7,972 ops/sec ±2.22% (84 runs sampled)
11,883 op/s ⨠ bencode
5,457 op/s ⨠ bencoding
6,550 op/s ⨠ dht_bencode
5,405 op/s ⨠ bncode
10,978 op/s ⨠ dht
```

@@ -51,11 +51,17 @@

```
bencode x 29,588 ops/sec ±0.75% (99 runs sampled)
bencoding x 27,844 ops/sec ±1.97% (95 runs sampled)
dht-bencode x 22,975 ops/sec ±1.84% (90 runs sampled)
bncode x 990 ops/sec ±2.71% (88 runs sampled)
dht.js x 19,103 ops/sec ±2.14% (89 runs sampled)
30,240 op/s ⨠ bencode
25,293 op/s ⨠ bencoding
26,220 op/s ⨠ dht_bencode
824 op/s ⨠ bncode
19,824 op/s ⨠ dht
```
*all benchmarks from an Intel Core2Duo@2.6GHz with node v0.10.3.*
*Benchmarks run on an Intel Core2 CPU T9550 @ 2.66GHz with node v0.10.5*
To run the benchmarks simply use
```
npm run-script bench
```
## Usage

@@ -62,0 +68,0 @@

@@ -9,3 +9,3 @@ var assert = require('assert')

assert.deepEqual(bencode.decode('i123e'), 123);
assert.deepEqual(bencode.decode('i-123e', 'utf8'), -123);
assert.deepEqual(bencode.decode('i-123e'), -123);
});

@@ -12,0 +12,0 @@ it('should be able to decode a float (as int)', function() {

@@ -39,2 +39,14 @@ var assert = require("assert");

})
it('should be able to encode a positive 64 bit int', function() {
assert.equal(bencode.encode(4777722361), 'i4777722361e');
})
it('should be able to encode a negative 64 bit int', function() {
assert.equal(bencode.encode(-4777722361), 'i-4777722361e');
})
it('should be able to encode a positive 64 bit float (as int)', function() {
assert.equal(bencode.encode(4777722361.5), 'i4777722361e');
})
it('should be able to encode a negative 64 bit float (as int)', function() {
assert.equal(bencode.encode(-4777722361.5), 'i-4777722361e');
})
it('should be able to encode a string', function() {

@@ -41,0 +53,0 @@ assert.equal(bencode.encode("asdf"), '4:asdf');

Sorry, the diff of this file is not supported yet

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