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
3 changes: 3 additions & 0 deletions .autod.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ module.exports = {
exclude: [
'test/fixtures',
],
dep: [
'long',
],
devdep: [
'autod',
'egg-bin',
Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: node_js
node_js:
- '8'
- '10'
- '11'
install:
- npm i npminstall && npminstall
script:
Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ environment:
matrix:
- nodejs_version: '8'
- nodejs_version: '10'
- nodejs_version: '11'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
5 changes: 5 additions & 0 deletions benchmark/require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

console.time('byte');
require('..');
console.timeEnd('byte');
22 changes: 16 additions & 6 deletions lib/byte.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
'use strict';

const Long = require('long');
const debug = require('debug')('byte');
const importLazy = require('import-lazy')(require);
const Long = importLazy('long');
const numbers = require('./number');
const utility = require('utility');
Copy link
Member Author

Choose a reason for hiding this comment

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

utility 加载要几十毫秒


const MAX_SAFE_INTEGER_STR = '9007199254740991';
const MAX_SAFE_INTEGER_STR_LENGTH = 16; // '9007199254740991'.length
const DEFAULT_SIZE = 1024;
const BIG_ENDIAN = 1;
const LITTLE_ENDIAN = 2;
const MAX_INT_31 = Math.pow(2, 31);
const ONE_HUNDRED_MB = 100 * 1024 * 1024;

function isSafeNumberString(s) {
if (s[0] === '-') {
s = s.substring(1);
}
if (s.length < MAX_SAFE_INTEGER_STR_LENGTH ||
(s.length === MAX_SAFE_INTEGER_STR_LENGTH && s <= MAX_SAFE_INTEGER_STR)) {
return true;
}
return false;
}

function ByteBuffer(options) {
options = options || {};
this._order = options.order || BIG_ENDIAN;
Expand Down Expand Up @@ -53,10 +65,8 @@ ByteBuffer.prototype._checkSize = function(afterSize) {
if (this._size >= afterSize) {
return;
}
const old = this._size;
this._size = afterSize * 2;
this._limit = this._size;
debug('allocate new Buffer: from %d to %d bytes', old, this._size);
const bytes = Buffer.alloc(this._size);
this._bytes.copy(bytes, 0);
this._bytes = bytes;
Expand Down Expand Up @@ -228,7 +238,7 @@ ByteBuffer.prototype.putLong = function(index, value) {

let isNumber = typeof value === 'number';
// convert safe number string to number
if (!isNumber && utility.isSafeNumberString(value)) {
if (!isNumber && isSafeNumberString(value)) {
isNumber = true;
value = Number(value);
}
Expand Down
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"scripts": {
"autod": "autod",
"lint": "eslint lib test *.js",
"lint": "eslint lib test --ext .js",
"test": "npm run lint && egg-bin test",
"test-local": "egg-bin test",
"cov": "egg-bin cov",
Expand All @@ -18,18 +18,17 @@
"optimized": "node --allow-natives-syntax --trace_opt --trace_deopt test/optimized.js"
},
"dependencies": {
"debug": "^3.1.0",
"long": "^4.0.0",
"utility": "^1.13.1"
"import-lazy": "^3.1.0",
"long": "^4.0.0"
},
"devDependencies": {
"autod": "^3.0.1",
"autod": "^3.1.0",
"beautify-benchmark": "^0.2.4",
"benchmark": "^2.1.4",
"egg-bin": "^4.7.0",
"egg-ci": "^1.8.0",
"eslint": "^4.19.1",
"eslint-config-egg": "^7.0.0",
"egg-bin": "^4.12.2",
"egg-ci": "^1.11.0",
"eslint": "^5.16.0",
"eslint-config-egg": "^7.3.1",
"fastbench": "^1.0.1",
"optimized": "^1.2.0"
},
Expand All @@ -54,7 +53,7 @@
"node": ">= 8.0.0"
},
"ci": {
"version": "8, 10"
"version": "8, 10, 11"
},
"author": "fengmk2 <[email protected]> (http://fengmk2.com)",
"license": "MIT"
Expand Down
46 changes: 23 additions & 23 deletions test/byte.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('byte.test.js', function() {
bytes.putString('');
bytes.putString('');
bytes.putString(null);
bytes.putString(new Buffer(''));
bytes.putString(Buffer.from(''));
bytes.putString(0, '');

bytes.position(0);
Expand All @@ -35,10 +35,10 @@ describe('byte.test.js', function() {
assert(bytes.getString(0) === 'foo, 中文');
bytes.putString(0, 'foo, 中国');
assert(bytes.getString(0) === 'foo, 中国');
bytes.putString(0, new Buffer('foo, 中国'));
bytes.putString(0, Buffer.from('foo, 中国'));
assert(bytes.getString(0) === 'foo, 中国');
bytes.putString('foo2');
assert(bytes.getString(new Buffer('foo, 中国').length + 4) === 'foo2');
assert(bytes.getString(Buffer.from('foo, 中国').length + 4) === 'foo2');

bytes.position(0);
assert(bytes.getString() === 'foo, 中国');
Expand All @@ -60,7 +60,7 @@ describe('byte.test.js', function() {
bytes.putCString('bar123123, \u0000中文\u0000');
bytes.putCString('foo2foo, \u0000中文\u0000');
assert(
bytes.getCString(new Buffer('bar123123, \u0000中文\u0000').length + 4 + 1) === 'foo2foo, \u0000中文\u0000'
bytes.getCString(Buffer.from('bar123123, \u0000中文\u0000').length + 4 + 1) === 'foo2foo, \u0000中文\u0000'
);

bytes.position(0);
Expand Down Expand Up @@ -440,21 +440,21 @@ describe('byte.test.js', function() {
assert(bytes.toString() === '<ByteBuffer 02 01>');
assert(bytes.get(1) === 1);

bytes.put(new Buffer([ 255, 255, 255 ]));
bytes.put(Buffer.from([ 255, 255, 255 ]));
assert(bytes.toString() === '<ByteBuffer 02 01 ff ff ff>');
assert.deepEqual(bytes.get(2, 3), new Buffer([ 255, 255, 255 ]));
assert.deepEqual(bytes.get(2, 3), Buffer.from([ 255, 255, 255 ]));

bytes.put(new Buffer([ 255, 254, 1 ]), 1, 2);
bytes.put(Buffer.from([ 255, 254, 1 ]), 1, 2);
assert(bytes.toString() === '<ByteBuffer 02 01 ff ff ff fe 01>');
assert.deepEqual(bytes.get(5, 2), new Buffer([ 254, 1 ]));
assert.deepEqual(bytes.get(5, 2), Buffer.from([ 254, 1 ]));

bytes.position(0);
assert.deepEqual(bytes.read(7), new Buffer([ 2, 1, 255, 255, 255, 254, 1 ]));
assert.deepEqual(bytes.read(7), Buffer.from([ 2, 1, 255, 255, 255, 254, 1 ]));
assert(bytes.position() === 7);

bytes.position(0);
bytes.skip(5);
assert.deepEqual(bytes.read(2), new Buffer([ 254, 1 ]));
assert.deepEqual(bytes.read(2), Buffer.from([ 254, 1 ]));
assert(bytes.position() === 7);
});
});
Expand Down Expand Up @@ -668,17 +668,17 @@ describe('byte.test.js', function() {
const bytes = ByteBuffer.allocate(8);
bytes.putInt(0);
bytes.putInt(1);
assert.deepEqual(bytes.copy(4), new Buffer([ 0, 0, 0, 1 ]));
assert.deepEqual(bytes.copy(4), Buffer.from([ 0, 0, 0, 1 ]));
});

it('should copy(start, end)', function() {
const bytes = ByteBuffer.allocate(9);
bytes.putInt(0);
bytes.putInt(1);
assert.deepEqual(bytes.copy(0, 8), new Buffer([ 0, 0, 0, 0, 0, 0, 0, 1 ]));
assert.deepEqual(bytes.copy(0, 9), new Buffer([ 0, 0, 0, 0, 0, 0, 0, 1 ]));
assert.deepEqual(bytes.copy(0, 4), new Buffer([ 0, 0, 0, 0 ]));
assert.deepEqual(bytes.copy(4, 8), new Buffer([ 0, 0, 0, 1 ]));
assert.deepEqual(bytes.copy(0, 8), Buffer.from([ 0, 0, 0, 0, 0, 0, 0, 1 ]));
assert.deepEqual(bytes.copy(0, 9), Buffer.from([ 0, 0, 0, 0, 0, 0, 0, 1 ]));
assert.deepEqual(bytes.copy(0, 4), Buffer.from([ 0, 0, 0, 0 ]));
assert.deepEqual(bytes.copy(4, 8), Buffer.from([ 0, 0, 0, 1 ]));
});
});

Expand Down Expand Up @@ -751,11 +751,11 @@ describe('byte.test.js', function() {
const bytes = ByteBuffer.allocate(4);
bytes.putInt(1);
bytes.flip(); // switch to read mode
const buf = new Buffer(4);
const buf = Buffer.alloc(4);
bytes.get(buf);
assert.deepEqual(buf, new Buffer([ 0, 0, 0, 1 ]));
assert.deepEqual(buf, Buffer.from([ 0, 0, 0, 1 ]));

const buf2 = new Buffer(1);
const buf2 = Buffer.alloc(1);
assert.throws(function() {
bytes.get(buf2);
}, null, 'BufferOverflowException');
Expand All @@ -766,11 +766,11 @@ describe('byte.test.js', function() {
bytes.putInt(1);
bytes.putInt(5);
bytes.flip(); // switch to read mode
const buf = new Buffer(4);
const buf = Buffer.alloc(4);
bytes.get(buf);
assert.deepEqual(buf, new Buffer([ 0, 0, 0, 1 ]));
assert.deepEqual(buf, Buffer.from([ 0, 0, 0, 1 ]));
bytes.get(buf);
assert.deepEqual(buf, new Buffer([ 0, 0, 0, 5 ]));
assert.deepEqual(buf, Buffer.from([ 0, 0, 0, 5 ]));
});
});

Expand Down Expand Up @@ -807,7 +807,7 @@ describe('byte.test.js', function() {
it('should fill hello', function() {
const str = 'hello';
bytes.putRawString(str);
const buff = new Buffer(10);
const buff = Buffer.alloc(10);
initBuff(buff);
const copied = bytes.memcpy(buff);

Expand All @@ -820,7 +820,7 @@ describe('byte.test.js', function() {
});

it('should fill hel', function() {
const buff = new Buffer(3);
const buff = Buffer.alloc(3);
initBuff(buff);
const copied = bytes.memcpy(buff);

Expand Down