Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid big parent in sliced string #7

Merged
merged 1 commit into from
Oct 29, 2018
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
27 changes: 27 additions & 0 deletions .autod.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

module.exports = {
write: true,
prefix: '^',
test: [
'test',
],
devdep: [
'egg-ci',
'egg-bin',
'mm',
'autod',
'eslint',
'eslint-config-egg',
],
exclude: [
'./test/fixtures',
'benchmark',
],
semver: [
'debug@3',
'egg-bin@1',
'eslint@4',
'eslint-config-egg@6',
],
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ run
!.eslintrc
!.gitignore
!.travis.yml
!.autod.conf.js
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ node_js:
- '4'
- '6'
- '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 @@ -3,6 +3,7 @@ environment:
- nodejs_version: '4'
- nodejs_version: '6'
- nodejs_version: '8'
- nodejs_version: '10'

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

const url = require('url');
const JSON = require('..');
const path = require('path');
const heapdump = require('heapdump');

// const address = url.parse('hsf://127.0.0.1:12200?xx=xx', true);

// const buf = JSON.encode(address);
// console.log(buf.toString('hex'));
const buf = Buffer.from('70726f746f636f6c7c6873663a7c736c61736865737c617574687c686f73747c3132372e302e302e313a31323230307c706f72747c31323230307c686f73746e616d657c3132372e302e302e317c686173687c7365617263687c3f78783d78787c71756572797c78787c78787c706174686e616d657c706174687c3f78783d78787c687265667c6873663a2f2f3132372e302e302e313a31323230303f78783d78785e5e5e5e24307c317c327c2d317c337c2d337c347c357c367c377c387c397c417c2d337c427c437c447c24457c465d7c477c2d337c487c497c4a7c4b5d', 'hex');
const o = JSON.decode(buf);

console.log(o);

process.o = o;
// const file = path.join(__dirname, Date.now() + '.heapsnapshot');
// console.log(file);
// heapdump.writeSnapshot(file);

heapdump.writeSnapshot(function(err, filename) {
console.log('dump written to', filename);
});

10 changes: 7 additions & 3 deletions lib/decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ class JSONDecoder {
}

_decodeString(str) {
return str.replace(REG_STR_REPLACER, a => DECODER_REPLACER[a]);
// avoid Parent in (sliced string)
// https://github.com/nodejs/help/issues/711
// https://stackoverflow.com/questions/31712808/how-to-force-javascript-to-deep-copy-a-string
const r = str.replace(REG_STR_REPLACER, a => DECODER_REPLACER[a]);
return (' ' + r).slice(1);
Copy link
Member

Choose a reason for hiding this comment

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

加空字符串 '' 有效么?

Copy link
Member

Choose a reason for hiding this comment

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

或者 String(r) 呢?

Copy link
Member Author

@gxcsoccer gxcsoccer Oct 29, 2018

Choose a reason for hiding this comment

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

都没效果,我试过,它居然会判断是否发生变化

Copy link
Member

@fengmk2 fengmk2 Oct 29, 2018

Choose a reason for hiding this comment

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

JSON.parse(JSON.stringify(r)) 也加上看看是否性能最差的。

}

_decodeDate(str) {
Expand Down Expand Up @@ -81,11 +85,11 @@ class JSONDecoder {
const token = this.tokens[this.tokensIndex];
if (token === ']') {
debug('--> unpack buffer end, %j', arr);
return new Buffer(arr);
return Buffer.from(arr);
}
arr.push(this._unpack());
}
return new Buffer(arr);
return Buffer.from(arr);
}
case '#': // error
{
Expand Down
2 changes: 1 addition & 1 deletion lib/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class JSONEncoder {
packed += `^${this._pack(ast)}`;

debug('pack the json => %s', packed);
return new Buffer(packed);
return Buffer.from(packed);
}

_pack(ast) {
Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"lib"
],
"scripts": {
"autod": "autod --check",
"autod": "autod",
"pkgfiles": "egg-bin pkgfiles --check",
"lint": "eslint --ext .js lib test",
"test": "npm run lint && npm run test-local",
"test-local": "egg-bin test",
"cov": "egg-bin cov",
"ci": "npm run autod && npm run lint && npm run cov"
"ci": "npm run autod -- --check && npm run lint && npm run cov"
},
"repository": {
"type": "git",
Expand All @@ -30,24 +30,24 @@
},
"homepage": "https://github.com/node-modules/serialize-json#readme",
"dependencies": {
"debug": "^3.0.1",
"is-type-of": "^1.2.0",
"utility": "^1.12.0"
"debug": "^3.2.6",
"is-type-of": "^1.2.1",
"utility": "^1.15.0"
},
"devDependencies": {
"autod": "^2.9.0",
"autod": "^3.0.1",
"beautify-benchmark": "^0.2.4",
"benchmark": "^2.1.4",
"egg-bin": "^1.11.1",
"egg-ci": "^1.8.0",
"eslint": "^4.7.2",
"eslint-config-egg": "^5.1.1",
"mm": "^2.2.0"
"egg-ci": "^1.10.0",
"eslint": "^4.19.1",
"eslint-config-egg": "^6.0.0",
"mm": "^2.4.1"
},
"engines": {
"node": ">= 4.0.0"
},
"ci": {
"version": "4, 6, 8"
"version": "4, 6, 8, 10, 11"
}
}
4 changes: 2 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('test/index.test.js', () => {
});

it('should encode & decode buffer', () => {
const buffer = new Buffer('hello world');
const buffer = Buffer.from('hello world');
const buf = JSON.encode(buffer);
assert(is.buffer(buf));
assert.deepEqual(JSON.decode(buf), buffer);
Expand Down Expand Up @@ -101,7 +101,7 @@ describe('test/index.test.js', () => {
f: null,
g: undefined,
h: new Date(),
i: new Buffer('this is a buffer'),
i: Buffer.from('this is a buffer'),
j: new Error('this is a error'),
};
let buf = JSON.encode(json);
Expand Down