diff --git a/.travis.yml b/.travis.yml index 5993a31..4f76f1f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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' diff --git a/benchmark/get_byte.js b/benchmark/get_byte.js index f7bcc17..53b77ec 100644 --- a/benchmark/get_byte.js +++ b/benchmark/get_byte.js @@ -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 }); @@ -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); @@ -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); }) @@ -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) diff --git a/lib/byte.js b/lib/byte.js index f29e6cf..61ce3d4 100644 --- a/lib/byte.js +++ b/lib/byte.js @@ -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; @@ -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; diff --git a/package.json b/package.json index 95af16e..e736897 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -25,7 +23,6 @@ "autod": "*", "beautify-benchmark": "0", "benchmark": "1", - "contributors": "*", "fastbench": "^1.0.1", "intelli-espower-loader": "^1.0.1", "istanbul": "*", @@ -56,4 +53,4 @@ }, "author": "fengmk2 (http://fengmk2.com)", "license": "MIT" -} \ No newline at end of file +} diff --git a/test/byte.test.js b/test/byte.test.js index d3cb1ba..71c78f8 100644 --- a/test/byte.test.js +++ b/test/byte.test.js @@ -430,6 +430,9 @@ describe('byte.test.js', function () { bytes.put(1); assert(bytes.toString() === ''); assert(bytes.get(0) === 1); + assert(bytes.getByte(0) === 1); + bytes.reset(); + assert(bytes.getByte() === 1); bytes.put(0, 2); assert(bytes.toString() === '');