Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
sudo: false
language: node_js
node_js:
- '0.12'
- '1'
- '2'
- '3'
- '4'
script: "npm run test-travis"
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
- '6'
- '8'
script: 'npm run test-travis'
after_script: 'npm i codecov && codecov'
33 changes: 25 additions & 8 deletions benchmark/get_byte.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ const suite = new Benchmark.Suite();

const buf = new Buffer([2, 1, 255, 255, 255, 254, 1]);

console.log('new OldByteBuffer({ array: buf }).get(): %d', new OldByteBuffer({ array: buf }).get());
console.log('new ByteBuffer({ array: buf }).get(): %d', new ByteBuffer({ array: buf }).get());
console.log('new OldByteBuffer({ array: buf }).get(0): %d', new OldByteBuffer({ array: buf }).get(0));
console.log('new ByteBuffer({ array: buf }).get(0): %d', new ByteBuffer({ array: buf }).get(0));
console.log('new ByteBuffer({ array: buf }).getByte(): %d', new ByteBuffer({ array: buf }).getByte());
console.log('new ByteBuffer({ array: buf }).getByte(0): %d', new ByteBuffer({ array: buf }).getByte(0));

suite
.add('old get()', function() {
const bytes = new OldByteBuffer({ array: buf });
Expand All @@ -47,6 +54,10 @@ suite
const bytes = new ByteBuffer({ array: buf });
bytes.get();
})
.add('getByte()', function() {
const bytes = new ByteBuffer({ array: buf });
bytes.getByte();
})
.add('old get(index)', function() {
const bytes = new OldByteBuffer({ array: buf });
bytes.get(0);
Expand All @@ -55,6 +66,10 @@ suite
const bytes = new ByteBuffer({ array: buf });
bytes.get(0);
})
.add('getByte(index)', function() {
const bytes = new ByteBuffer({ array: buf });
bytes.getByte(0);
})
.on('cycle', function(event) {
benchmarks.add(event.target);
})
Expand All @@ -66,11 +81,13 @@ suite
})
.run({ 'async': false });

// node version: v7.10.0, date: Thu May 11 2017 02:58:46 GMT+0800 (CST)
// Starting...
// 4 tests completed.

// old get() x 2,234,273 ops/sec ±1.45% (90 runs sampled)
// new get() x 24,872,342 ops/sec ±2.13% (89 runs sampled)
// old get(index) x 2,339,452 ops/sec ±0.81% (93 runs sampled)
// new get(index) x 25,533,534 ops/sec ±0.84% (100 runs sampled)
// node version: v8.2.1, date: Mon Aug 07 2017 15:38:34 GMT+0800 (CST)
// Starting...
// 6 tests completed.
//
// old get() x 7,206,620 ops/sec ±8.81% (76 runs sampled)
// new get() x 13,628,654 ops/sec ±15.71% (72 runs sampled)
// getByte() x 18,050,339 ops/sec ±14.48% (76 runs sampled)
// old get(index) x 6,989,958 ops/sec ±9.77% (75 runs sampled)
// new get(index) x 13,580,238 ops/sec ±10.13% (72 runs sampled)
// getByte(index) x 16,031,923 ops/sec ±14.93% (62 runs sampled)
17 changes: 16 additions & 1 deletion lib/byte.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

var Long = require('long');
var debug = require('debug')('byte');
var numbers = require('./number');
var utility = require('utility');
var numbers = require('./number');

var DEFAULT_SIZE = 1024;
var BIG_ENDIAN = 1;
Expand Down Expand Up @@ -95,18 +95,33 @@ ByteBuffer.prototype.put = function (src, offset, length) {
return this;
};

// hign performence method instead of `get()`
ByteBuffer.prototype.getByte = function (index) {
if (arguments.length === 0) {
// getByte()
return this._bytes[this._offset++];
}

// getByte(index)
return this._bytes[index];
};

ByteBuffer.prototype.get = function (index, length) {
// get()
if (index == null && length == null) {
return this._bytes[this._offset++];
}
// get(index)
if (typeof index === 'number' && length == null) {
return this._bytes[index];
}
// get(index, length)
if (typeof index === 'number' && typeof length === 'number') {
// offset, length => Buffer
return this._copy(index, index + length);
}

// get(dst, offset)
var dst = index;
var offset = length || 0;
length = dst.length;
Expand Down
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
"test-cov": "node node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- -t 5000 test/*.test.js",
"test-travis": "node node_modules/.bin/istanbul cover node_modules/.bin/_mocha --report lcovonly -- -t 5000 test/*.test.js",
"jshint": "jshint .",
"autod": "autod -w --prefix '^' -e benchmark && npm run cnpm",
"cnpm": "npm install --registry=https://registry.npm.taobao.org",
"contributors": "contributors -f plain -o AUTHORS",
"autod": "autod -w --prefix '^' -e benchmark",
"optimized": "node --allow-natives-syntax --trace_opt --trace_deopt test/optimized.js"
},
"dependencies": {
Expand All @@ -25,7 +23,6 @@
"autod": "*",
"beautify-benchmark": "0",
"benchmark": "1",
"contributors": "*",
"fastbench": "^1.0.1",
"intelli-espower-loader": "^1.0.1",
"istanbul": "*",
Expand Down Expand Up @@ -56,4 +53,4 @@
},
"author": "fengmk2 <[email protected]> (http://fengmk2.com)",
"license": "MIT"
}
}
3 changes: 3 additions & 0 deletions test/byte.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ describe('byte.test.js', function () {
bytes.put(1);
assert(bytes.toString() === '<ByteBuffer 01>');
assert(bytes.get(0) === 1);
assert(bytes.getByte(0) === 1);
bytes.reset();
assert(bytes.getByte() === 1);

bytes.put(0, 2);
assert(bytes.toString() === '<ByteBuffer 02>');
Expand Down