Skip to content

Commit cba5ecf

Browse files
authored
Merge pull request #7 from msgpack/rewrite
Rewrite implementation in TypeScript
2 parents 2cfda99 + b711cd8 commit cba5ecf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1756
-30634
lines changed

.eslintrc.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
module.exports = {
2+
extends: [
3+
// https://eslint.org/docs/rules/
4+
"eslint:recommended",
5+
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/src/configs/recommended.json
6+
"plugin:@typescript-eslint/recommended",
7+
// https://prettier.io/docs/en/eslint.html
8+
"plugin:prettier/recommended",
9+
],
10+
// https://reactjs.org/docs/hooks-rules.html
11+
plugins: [],
12+
parser: "@typescript-eslint/parser",
13+
parserOptions: {
14+
project: "./tsconfig.json",
15+
},
16+
settings: {},
17+
rules: {
18+
"no-undef": "off", // useless in TypeScript
19+
"no-constant-condition": ["warn", { checkLoops: false }],
20+
"no-useless-escape": "warn",
21+
"no-console": "warn",
22+
23+
"@typescript-eslint/no-unused-vars": ["warn", { argsIgnorePattern: "^_|^intl$" }],
24+
"@typescript-eslint/array-type": ["error", "generic"],
25+
"@typescript-eslint/camelcase": "warn",
26+
"@typescript-eslint/class-name-casing": "warn", // to allow the initial underscore
27+
"@typescript-eslint/no-non-null-assertion": "warn", // NOTE: pay attention to it because it may cause unexpected behavior
28+
"@typescript-eslint/prefer-for-of": "warn",
29+
"@typescript-eslint/prefer-includes": "warn",
30+
"@typescript-eslint/prefer-string-starts-ends-with": "warn",
31+
"@typescript-eslint/no-use-before-define": "warn",
32+
33+
"@typescript-eslint/indent": "off",
34+
"@typescript-eslint/no-explicit-any": "off",
35+
"@typescript-eslint/explicit-function-return-type": "off",
36+
"@typescript-eslint/explicit-member-accessibility": "off",
37+
"@typescript-eslint/no-object-literal-type-assertion": "off",
38+
"@typescript-eslint/no-empty-interface": "off",
39+
"@typescript-eslint/no-parameter-properties": "off",
40+
"@typescript-eslint/no-var-requires": "off", // not a part of ECMA-262
41+
"@typescript-eslint/prefer-interface": "off",
42+
43+
"prettier/prettier": "warn",
44+
},
45+
};

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
node_modules/
2+
dist/
3+
build/
4+
package-lock.json
5+
.nyc_output/
6+
coverage/
7+
8+
# v8 profiler logs
9+
isolate-*.log
10+
11+
# flamebearer
12+
flamegraph.html

.nycrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"include": ["src/**/*.ts"],
3+
"extension": [".ts"],
4+
"reporter": ["text-summary", "html"],
5+
"sourceMap": true,
6+
"instrument": true
7+
}

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- "10"
4+
- "12"
5+
cache: npm

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "node_modules/typescript/lib"
3+
}

LICENSE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Copyright 2019 The MessagePack Community.
2+
3+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
4+
5+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

README

Whitespace-only changes.

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# MessagePack for JavaScript [![Build Status](https://travis-ci.org/msgpack/msgpack-javascript.svg?branch=master)](https://travis-ci.org/msgpack/msgpack-javascript)
2+
3+
This is the pure-JavaScript implementation of MessagePack:
4+
5+
https://msgpack.org/
6+
7+
## Usage
8+
9+
TBD
10+
11+
## Install
12+
13+
```shell
14+
npm install @msgpack/msgpack
15+
```
16+
17+
## License
18+
19+
Copyright 2019 The MessagePack Community.
20+
21+
This software is licensed under the ISC license:
22+
23+
https://opensource.org/licenses/ISC
24+
25+
See [LICENSE](./LICENSE) for details.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"int0": 0,
3+
"int1": 1,
4+
"int1-": -1,
5+
"int8": 255,
6+
"int8-": -255,
7+
"int16": 256,
8+
"int16-": -256,
9+
"int32": 65536,
10+
"int32-": -65536,
11+
"nil": null,
12+
"true": true,
13+
"false": false,
14+
"float": 0.5,
15+
"float-": -0.5,
16+
"string0": "",
17+
"string1": "A",
18+
"string4": "foobarbaz",
19+
"string8": "Omnes viae Romam ducunt.",
20+
"string16": "L’homme n’est qu’un roseau, le plus faible de la nature ; mais c’est un roseau pensant. Il ne faut pas que l’univers entier s’arme pour l’écraser : une vapeur, une goutte d’eau, suffit pour le tuer. Mais, quand l’univers l’écraserait, l’homme serait encore plus noble que ce qui le tue, puisqu’il sait qu’il meurt, et l’avantage que l’univers a sur lui, l’univers n’en sait rien. Toute notre dignité consiste donc en la pensée. C’est de là qu’il faut nous relever et non de l’espace et de la durée, que nous ne saurions remplir. Travaillons donc à bien penser : voilà le principe de la morale.",
21+
"array0": [],
22+
"array1": [
23+
"foo"
24+
],
25+
"array8": [
26+
1,
27+
2,
28+
4,
29+
8,
30+
16,
31+
32,
32+
64,
33+
128,
34+
256,
35+
512,
36+
1024,
37+
2048,
38+
4096,
39+
8192,
40+
16384,
41+
32768,
42+
65536,
43+
131072,
44+
262144,
45+
524288,
46+
1048576
47+
],
48+
"map0": {},
49+
"map1": {
50+
"foo": "bar"
51+
}
52+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// original: https://raw.githubusercontent.com/kawanet/msgpack-lite/master/lib/benchmark.js
2+
3+
// automatically use the dist version if available
4+
var msgpack_msgpack = try_require("..") || require("../src");
5+
6+
var msgpack_node = try_require("msgpack");
7+
var msgpack_lite = try_require("msgpack-lite");
8+
var msgpack_js = try_require("msgpack-js");
9+
var msgpack_js_v5 = try_require("msgpack-js-v5");
10+
var msgpack5 = try_require("msgpack5");
11+
var msgpack_unpack = try_require("msgpack-unpack");
12+
var notepack = try_require("notepack");
13+
14+
msgpack5 = msgpack5 && msgpack5();
15+
16+
var pkg = require("../package.json");
17+
var data = require("./benchmark-from-msgpack-lite-data.json");
18+
var packed = msgpack_lite.encode(data);
19+
var expected = JSON.stringify(data);
20+
21+
var argv = Array.prototype.slice.call(process.argv, 2);
22+
23+
if (argv[0] === "-v") {
24+
console.warn(pkg.name + " " + pkg.version);
25+
process.exit(0);
26+
}
27+
28+
var limit = 5;
29+
if (argv[0] - 0) limit = argv.shift() - 0;
30+
limit *= 1000;
31+
32+
var COL1 = 65;
33+
var COL2 = 7;
34+
var COL3 = 5;
35+
var COL4 = 7;
36+
37+
console.log(`Benchmark on NodeJS/${process.version}\n`)
38+
console.log(rpad("operation", COL1), "|", " op ", "|", " ms ", "|", " op/s ");
39+
console.log(rpad("", COL1, "-"), "|", lpad(":", COL2, "-"), "|", lpad(":", COL3, "-"), "|", lpad(":", COL4, "-"));
40+
41+
var buf, obj;
42+
43+
if (JSON) {
44+
buf = bench('buf = Buffer.from(JSON.stringify(obj));', JSON_stringify, data);
45+
buf = bench('buf = JSON.stringify(obj);', JSON.stringify, data);
46+
obj = bench('obj = JSON.parse(buf);', JSON.parse, buf);
47+
runTest(obj);
48+
}
49+
50+
if (msgpack_lite) {
51+
buf = bench('buf = require("msgpack-lite").encode(obj);', msgpack_lite.encode, data);
52+
obj = bench('obj = require("msgpack-lite").decode(buf);', msgpack_lite.decode, packed);
53+
runTest(obj);
54+
}
55+
56+
if (msgpack_node) {
57+
buf = bench('buf = require("msgpack").pack(obj);', msgpack_node.pack, data);
58+
obj = bench('obj = require("msgpack").unpack(buf);', msgpack_node.unpack, buf);
59+
runTest(obj);
60+
}
61+
62+
if (msgpack_msgpack) {
63+
buf = bench('buf = require("@msgpack/msgpack").encode(obj);', msgpack_msgpack.encode, data);
64+
buf = bench('buf = Buffer.from(require("@msgpack/msgpack").encode(obj));', msgpack_msgpack_encode, data);
65+
obj = bench('obj = require("@msgpack/msgpack").decode(buf);', msgpack_msgpack.decode, buf);
66+
runTest(obj);
67+
}
68+
69+
if (msgpack_js_v5) {
70+
buf = bench('buf = require("msgpack-js-v5").encode(obj);', msgpack_js_v5.encode, data);
71+
obj = bench('obj = require("msgpack-js-v5").decode(buf);', msgpack_js_v5.decode, buf);
72+
runTest(obj);
73+
}
74+
75+
if (msgpack_js) {
76+
buf = bench('buf = require("msgpack-js").encode(obj);', msgpack_js.encode, data);
77+
obj = bench('obj = require("msgpack-js").decode(buf);', msgpack_js.decode, buf);
78+
runTest(obj);
79+
}
80+
81+
if (msgpack5) {
82+
buf = bench('buf = require("msgpack5")().encode(obj);', msgpack5.encode, data);
83+
obj = bench('obj = require("msgpack5")().decode(buf);', msgpack5.decode, buf);
84+
runTest(obj);
85+
}
86+
87+
if (notepack) {
88+
buf = bench('buf = require("notepack").encode(obj);', notepack.encode, data);
89+
obj = bench('obj = require("notepack").decode(buf);', notepack.decode, buf);
90+
runTest(obj);
91+
}
92+
93+
if (msgpack_unpack) {
94+
obj = bench('obj = require("msgpack-unpack").decode(buf);', msgpack_unpack, packed);
95+
runTest(obj);
96+
}
97+
98+
function JSON_stringify(src: any) {
99+
return Buffer.from(JSON.stringify(src));
100+
}
101+
102+
function msgpack_msgpack_encode(data: any) {
103+
return Buffer.from(msgpack_msgpack.encode(data));
104+
}
105+
106+
function bench(name: string, func: (...args: any[]) => any, src: any) {
107+
if (argv.length) {
108+
var match = argv.filter(function(grep) {
109+
return (name.indexOf(grep) > -1);
110+
});
111+
if (!match.length) return SKIP;
112+
}
113+
// warm up
114+
func(src);
115+
116+
var ret, duration: number;
117+
var start = Date.now();
118+
var count = 0;
119+
while (1) {
120+
var end = Date.now();
121+
duration = end - start;
122+
if (duration >= limit) break;
123+
while ((++count) % 100) ret = func(src);
124+
}
125+
name = rpad(name, COL1);
126+
var score = Math.floor(count / duration! * 1000);
127+
console.log(name, "|", lpad(`${count}`, COL2), "|", lpad(`${duration}`, COL3), "|", lpad(`${score}`, COL4));
128+
return ret;
129+
}
130+
131+
function rpad(str: string, len: number, chr = " ") {
132+
return str.padEnd(len, chr);
133+
}
134+
135+
function lpad(str: string, len: number, chr = " ") {
136+
return str.padStart(len, chr);
137+
}
138+
139+
function runTest(actual: any) {
140+
if (actual === SKIP) return;
141+
actual = JSON.stringify(actual);
142+
if (actual === expected) return;
143+
console.warn("expected: " + expected);
144+
console.warn("actual: " + actual);
145+
}
146+
147+
function SKIP() {
148+
}
149+
150+
function try_require(name: string) {
151+
try {
152+
return require(name);
153+
} catch (e) {
154+
// ignore
155+
}
156+
}

0 commit comments

Comments
 (0)