msgpack.js RC2
Change Log
- Retake toIEEE754()
- Retake callback arguments in msgpack.download() and msgpack.download()
Functions
- Download and Upload functions
- msgpack.download(url, option, callback) -> download binary data by GET method.
- msgpack.upload(url, option, callback) -> upload base64 data by PUT method.
- Pack and Unpack functions
- msgpack.pack(Mix) is converted into ByteArray of the MessagePack format.
- Mix are undefined, null, Object, Array, String and Number types.
- ByteArray is [0x00, 0xff, ...] alias.
- msgpack.unpack(ByteArray):Mix is restored.
- msgpack.pack(Mix) is converted into ByteArray of the MessagePack format.
- Support WebWorkers
- msgpack.download(url, { worker: true }, callback)
- msgpack.upload(url, { worker: true }, callback)
- msgpack.worker - String(= "msgpack.js"): WebWorkers script URL
- Support Browsers
- A Grade: Google Chrome 4+, Safari5+, Firefox3.6+, Opera10.50+, iPhone 3G+, IE8+
- B Grade: Safari3.1+, Firefox3+, Opera9.6+, IE6+
Sorce Code + Testing environment
http://pigs.sourceforge.jp/blog/msgpack/20100618/msgpack.js
http://pigs.sourceforge.jp/blog/msgpack/20100618/msgpack-20100618.js.zip
Benchmark (JSON vs msgpack)
(unit = ms)
(JSON = JSON.stringify + JSON.parse)
(msgpack = msgpack.pack + msgpack.unpack)
Create 100000
JSON | msgpack | rario(RC2) | ratio(RC1) | |
Chrome 6(dev) | 3883 | 2055 | 190% | 170% |
Safari 5 | 682 | 2762 | 25% | 21% |
Firefox 3.6.3 | 822 | 5805 | 14% | --- |
Opera 10.60β(3422) | 510 | 1700 | 30% | 25% |
How to use
// msgpack.pack(), msgpack.unpack()
<script src="msgpack.js"></script> <script> var byteArray = msgpack.pack({ hello: "world" }); alert( msgpack.unpack(byteArray).hello ); // world </script>
// msgpack.download() with WebWorkers
<script src="msgpack.js"></script> <script> function callback(data, option, response) { if (response.ok) { alert( data ); // Mix (JavaScript Object) } else { alert( "ERROR STATUS = " + response.status ); // xhr.status } } // set worker script path // msgpack.worker = "http://example.com/js/msgpack.js"; msgpack.download(url, { worker: true }, callback); </script>
// msgpack.upload() with WebWorkers
<script src="msgpack.js"></script> <script> function callback(data, option, response) { if (response.ok) { alert( data ); // xhr.responseText (optional) } else { alert( "ERROR STATUS = " + response.status ); // xhr.status } } // set worker script path // msgpack.worker = "http://example.com/js/msgpack.js"; var mix = { hello: "world" }; msgpack.upload("http://example.com/upload.php", { worker: true, data: mix, timeout: 5 }, callback); </script>
// upload.php
<?php // see http://php.net/manual/en/features.file-upload.put-method.php $data = fopen("php://input", "r"); $fp = fopen("msgpack.bin", "w"); // output filename while ($part = fread($data, 8192)) { // 8192 bytes fwrite($fp, base64_decode($part)); // base64 decode } fclose($fp); fclose($data); print_r(getallheaders()); ?>
反省会
- 「勢いしか感じられない」テイストだった toIEEE754() 相当の処理を入れ替えました。
- 中身は edvakf さんのコードほぼそのまま参考にさせてもらいました。edvakf さん。ありがとうございます。
- msgpack.download() と msgpack.download() の callback引数を変えました。
- IEEE754 変換処理の差し替え、GC発生抑止、inline展開、名前解決速度の向上により、ちょっと速くなってます。
// --- minify --- _0x100000000000000 = 0x100000000000000, _0x1000000000000 = 0x1000000000000, _0x10000000000 = 0x10000000000, _0x100000000 = 0x100000000, _0x1000000 = 0x1000000, _0x10000 = 0x10000, _0x100 = 0x100, _0xff = /* */ 0xff, _0x80 = 0x80, _0x8000 = 0x8000, _0x80000000 = 0x80000000, _00000000000 = "00000000000",