Skip to content

Rewrite implementation in TypeScript #7

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

Merged
merged 28 commits into from
May 2, 2019
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
45 changes: 45 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module.exports = {
extends: [
// https://eslint.org/docs/rules/
"eslint:recommended",
// https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/src/configs/recommended.json
"plugin:@typescript-eslint/recommended",
// https://prettier.io/docs/en/eslint.html
"plugin:prettier/recommended",
],
// https://reactjs.org/docs/hooks-rules.html
plugins: [],
parser: "@typescript-eslint/parser",
parserOptions: {
project: "./tsconfig.json",
},
settings: {},
rules: {
"no-undef": "off", // useless in TypeScript
"no-constant-condition": ["warn", { checkLoops: false }],
"no-useless-escape": "warn",
"no-console": "warn",

"@typescript-eslint/no-unused-vars": ["warn", { argsIgnorePattern: "^_|^intl$" }],
"@typescript-eslint/array-type": ["error", "generic"],
"@typescript-eslint/camelcase": "warn",
"@typescript-eslint/class-name-casing": "warn", // to allow the initial underscore
"@typescript-eslint/no-non-null-assertion": "warn", // NOTE: pay attention to it because it may cause unexpected behavior
"@typescript-eslint/prefer-for-of": "warn",
"@typescript-eslint/prefer-includes": "warn",
"@typescript-eslint/prefer-string-starts-ends-with": "warn",
"@typescript-eslint/no-use-before-define": "warn",

"@typescript-eslint/indent": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-member-accessibility": "off",
"@typescript-eslint/no-object-literal-type-assertion": "off",
"@typescript-eslint/no-empty-interface": "off",
"@typescript-eslint/no-parameter-properties": "off",
"@typescript-eslint/no-var-requires": "off", // not a part of ECMA-262
"@typescript-eslint/prefer-interface": "off",

"prettier/prettier": "warn",
},
};
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
node_modules/
dist/
build/
package-lock.json
.nyc_output/
coverage/

# v8 profiler logs
isolate-*.log

# flamebearer
flamegraph.html
7 changes: 7 additions & 0 deletions .nycrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"include": ["src/**/*.ts"],
"extension": [".ts"],
"reporter": ["text-summary", "html"],
"sourceMap": true,
"instrument": true
}
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: node_js
node_js:
- "10"
- "12"
cache: npm
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
5 changes: 5 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Copyright 2019 The MessagePack Community.

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.

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.
Empty file removed README
Empty file.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# MessagePack for JavaScript [![Build Status](https://travis-ci.org/msgpack/msgpack-javascript.svg?branch=master)](https://travis-ci.org/msgpack/msgpack-javascript)

This is the pure-JavaScript implementation of MessagePack:

https://msgpack.org/

## Usage

TBD

## Install

```shell
npm install @msgpack/msgpack
```

## License

Copyright 2019 The MessagePack Community.

This software is licensed under the ISC license:

https://opensource.org/licenses/ISC

See [LICENSE](./LICENSE) for details.
52 changes: 52 additions & 0 deletions benchmark/benchmark-from-msgpack-lite-data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"int0": 0,
"int1": 1,
"int1-": -1,
"int8": 255,
"int8-": -255,
"int16": 256,
"int16-": -256,
"int32": 65536,
"int32-": -65536,
"nil": null,
"true": true,
"false": false,
"float": 0.5,
"float-": -0.5,
"string0": "",
"string1": "A",
"string4": "foobarbaz",
"string8": "Omnes viae Romam ducunt.",
"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.",
"array0": [],
"array1": [
"foo"
],
"array8": [
1,
2,
4,
8,
16,
32,
64,
128,
256,
512,
1024,
2048,
4096,
8192,
16384,
32768,
65536,
131072,
262144,
524288,
1048576
],
"map0": {},
"map1": {
"foo": "bar"
}
}
156 changes: 156 additions & 0 deletions benchmark/benchmark-from-msgpack-lite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// original: https://raw.githubusercontent.com/kawanet/msgpack-lite/master/lib/benchmark.js

// automatically use the dist version if available
var msgpack_msgpack = try_require("..") || require("../src");

var msgpack_node = try_require("msgpack");
var msgpack_lite = try_require("msgpack-lite");
var msgpack_js = try_require("msgpack-js");
var msgpack_js_v5 = try_require("msgpack-js-v5");
var msgpack5 = try_require("msgpack5");
var msgpack_unpack = try_require("msgpack-unpack");
var notepack = try_require("notepack");

msgpack5 = msgpack5 && msgpack5();

var pkg = require("../package.json");
var data = require("./benchmark-from-msgpack-lite-data.json");
var packed = msgpack_lite.encode(data);
var expected = JSON.stringify(data);

var argv = Array.prototype.slice.call(process.argv, 2);

if (argv[0] === "-v") {
console.warn(pkg.name + " " + pkg.version);
process.exit(0);
}

var limit = 5;
if (argv[0] - 0) limit = argv.shift() - 0;
limit *= 1000;

var COL1 = 65;
var COL2 = 7;
var COL3 = 5;
var COL4 = 7;

console.log(`Benchmark on NodeJS/${process.version}\n`)
console.log(rpad("operation", COL1), "|", " op ", "|", " ms ", "|", " op/s ");
console.log(rpad("", COL1, "-"), "|", lpad(":", COL2, "-"), "|", lpad(":", COL3, "-"), "|", lpad(":", COL4, "-"));

var buf, obj;

if (JSON) {
buf = bench('buf = Buffer.from(JSON.stringify(obj));', JSON_stringify, data);
buf = bench('buf = JSON.stringify(obj);', JSON.stringify, data);
obj = bench('obj = JSON.parse(buf);', JSON.parse, buf);
runTest(obj);
}

if (msgpack_lite) {
buf = bench('buf = require("msgpack-lite").encode(obj);', msgpack_lite.encode, data);
obj = bench('obj = require("msgpack-lite").decode(buf);', msgpack_lite.decode, packed);
runTest(obj);
}

if (msgpack_node) {
buf = bench('buf = require("msgpack").pack(obj);', msgpack_node.pack, data);
obj = bench('obj = require("msgpack").unpack(buf);', msgpack_node.unpack, buf);
runTest(obj);
}

if (msgpack_msgpack) {
buf = bench('buf = require("@msgpack/msgpack").encode(obj);', msgpack_msgpack.encode, data);
buf = bench('buf = Buffer.from(require("@msgpack/msgpack").encode(obj));', msgpack_msgpack_encode, data);
obj = bench('obj = require("@msgpack/msgpack").decode(buf);', msgpack_msgpack.decode, buf);
runTest(obj);
}

if (msgpack_js_v5) {
buf = bench('buf = require("msgpack-js-v5").encode(obj);', msgpack_js_v5.encode, data);
obj = bench('obj = require("msgpack-js-v5").decode(buf);', msgpack_js_v5.decode, buf);
runTest(obj);
}

if (msgpack_js) {
buf = bench('buf = require("msgpack-js").encode(obj);', msgpack_js.encode, data);
obj = bench('obj = require("msgpack-js").decode(buf);', msgpack_js.decode, buf);
runTest(obj);
}

if (msgpack5) {
buf = bench('buf = require("msgpack5")().encode(obj);', msgpack5.encode, data);
obj = bench('obj = require("msgpack5")().decode(buf);', msgpack5.decode, buf);
runTest(obj);
}

if (notepack) {
buf = bench('buf = require("notepack").encode(obj);', notepack.encode, data);
obj = bench('obj = require("notepack").decode(buf);', notepack.decode, buf);
runTest(obj);
}

if (msgpack_unpack) {
obj = bench('obj = require("msgpack-unpack").decode(buf);', msgpack_unpack, packed);
runTest(obj);
}

function JSON_stringify(src: any) {
return Buffer.from(JSON.stringify(src));
}

function msgpack_msgpack_encode(data: any) {
return Buffer.from(msgpack_msgpack.encode(data));
}

function bench(name: string, func: (...args: any[]) => any, src: any) {
if (argv.length) {
var match = argv.filter(function(grep) {
return (name.indexOf(grep) > -1);
});
if (!match.length) return SKIP;
}
// warm up
func(src);

var ret, duration: number;
var start = Date.now();
var count = 0;
while (1) {
var end = Date.now();
duration = end - start;
if (duration >= limit) break;
while ((++count) % 100) ret = func(src);
}
name = rpad(name, COL1);
var score = Math.floor(count / duration! * 1000);
console.log(name, "|", lpad(`${count}`, COL2), "|", lpad(`${duration}`, COL3), "|", lpad(`${score}`, COL4));
return ret;
}

function rpad(str: string, len: number, chr = " ") {
return str.padEnd(len, chr);
}

function lpad(str: string, len: number, chr = " ") {
return str.padStart(len, chr);
}

function runTest(actual: any) {
if (actual === SKIP) return;
actual = JSON.stringify(actual);
if (actual === expected) return;
console.warn("expected: " + expected);
console.warn("actual: " + actual);
}

function SKIP() {
}

function try_require(name: string) {
try {
return require(name);
} catch (e) {
// ignore
}
}
16 changes: 16 additions & 0 deletions benchmark/profile-decode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { encode, decode } from "../src";

const data = require("./benchmark-from-msgpack-lite-data.json");
const encoded = encode(data);

console.time("decode #1");
for (let i = 0; i < 10000; i++) {
decode(encoded);
}
console.timeEnd("decode #1");

console.time("decode #2");
for (let i = 0; i < 10000; i++) {
decode(encoded);
}
console.timeEnd("decode #2");
15 changes: 15 additions & 0 deletions benchmark/profile-encode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { encode } from "../src";

const data = require("./benchmark-from-msgpack-lite-data.json");

console.time("encode #1");
for (let i = 0; i < 10000; i++) {
encode(data);
}
console.timeEnd("encode #1");

console.time("encode #2");
for (let i = 0; i < 10000; i++) {
encode(data);
}
console.timeEnd("encode #2");
Loading