Skip to content
Merged
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
76 changes: 76 additions & 0 deletions benchmark/get_byte.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use strict';

const Benchmark = require('benchmark');
const benchmarks = require('beautify-benchmark');
const ByteBuffer = require('../');

class OldByteBuffer extends ByteBuffer {
get(index, length) {
if (index instanceof Buffer || Array.isArray(index)) { // get (byte[] dst, int offset, int length)
var dst = index;
var offset = length || 0;
length = dst.length;

this.checkArraySize(dst.length, offset, length);
this.checkForUnderflow(length);

if (dst instanceof Buffer) {
this._bytes.copy(dst, offset, this._offset, (this._offset + length));
} else {
for (var i = offset; i < (offset + length); i++) {
dst[i] = this._bytes[i];
}
}
this._offset += length;
return this;
} else if (typeof index !== 'number') {
index = this._offset++;
} else if (typeof length === 'number') {
// offset, length => Buffer
return this._copy(index, index + length);
}
// return byte
return this._bytes[index];
}
}

const suite = new Benchmark.Suite();

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

suite
.add('old get()', function() {
const bytes = new OldByteBuffer({ array: buf });
bytes.get();
})
.add('new get()', function() {
const bytes = new ByteBuffer({ array: buf });
bytes.get();
})
.add('old get(index)', function() {
const bytes = new OldByteBuffer({ array: buf });
bytes.get(0);
})
.add('new get(index)', function() {
const bytes = new ByteBuffer({ array: buf });
bytes.get(0);
})
.on('cycle', function(event) {
benchmarks.add(event.target);
})
.on('start', function(event) {
console.log('\n node version: %s, date: %s\n Starting...', process.version, Date());
})
.on('complete', function done() {
benchmarks.log();
})
.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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这性能优化有点吊。。。一个数量级。

44 changes: 23 additions & 21 deletions lib/byte.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,31 +105,33 @@ ByteBuffer.prototype.put = function (src, offset, length) {
};

ByteBuffer.prototype.get = function (index, length) {
if (index instanceof Buffer || Array.isArray(index)) { // get (byte[] dst, int offset, int length)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个判断比较耗时,把它挪到后面去,在 hessian.js 里大部分情况是 get() 或者 get(index)

var dst = index;
var offset = length || 0;
length = dst.length;
if (index == null && length == null) {
return this._bytes[this._offset++];
}
if (typeof index === 'number' && length == null) {
return this._bytes[index];
}
if (typeof index === 'number' && typeof length === 'number') {
// offset, length => Buffer
return this._copy(index, index + length);
}

this.checkArraySize(dst.length, offset, length);
this.checkForUnderflow(length);
var dst = index;
var offset = length || 0;
length = dst.length;

if (dst instanceof Buffer) {
this._bytes.copy(dst, offset, this._offset, (this._offset + length));
} else {
for (var i = offset; i < (offset + length); i++) {
dst[i] = this._bytes[i];
}
this.checkArraySize(dst.length, offset, length);
this.checkForUnderflow(length);

if (Buffer.isBuffer(dst)) {
this._bytes.copy(dst, offset, this._offset, (this._offset + length));
} else {
for (var i = offset; i < (offset + length); i++) {
dst[i] = this._bytes[i];
}
this._offset += length;
return this;
} else if (typeof index !== 'number') {
index = this._offset++;
} else if (typeof length === 'number') {
// offset, length => Buffer
return this._copy(index, index + length);
}
// return byte
return this._bytes[index];
this._offset += length;
return this;
};

ByteBuffer.prototype.read = function (size) {
Expand Down
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
"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",
"autod": "autod -w --prefix '^' -e benchmark && npm run cnpm",
"cnpm": "npm install --registry=https://registry.npm.taobao.org",
"contributors": "contributors -f plain -o AUTHORS",
"optimized": "node --allow-natives-syntax --trace_opt --trace_deopt test/optimized.js"
},
"dependencies": {
"debug": "~2.2.0",
"long": "~2.2.5",
"utility": "~1.4.0"
"debug": "^2.6.6",
"long": "^3.2.0",
"utility": "^1.12.0"
},
"devDependencies": {
"autod": "*",
Expand All @@ -29,8 +29,7 @@
"istanbul": "*",
"jshint": "*",
"mocha": "*",
"optimized": "1",
"should": "7"
"optimized": "^1.2.0"
},
"homepage": "https://github.com/node-modules/byte",
"repository": {
Expand Down
Loading