为什么在node.js中JSON比BSON更快? [英] Why is JSON faster than BSON in node.js?

查看:174
本文介绍了为什么在node.js中JSON比BSON更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读后,有一条引人注目的报价:

After reading this, there is a quote that stood out:

BSON还被设计为可以快速编码和解码.例如,整数存储为32(或64)位整数,因此不需要在文本之间进行解析.对于小整数,此方​​法比JSON使用更多的空间,但解析起来要快得多.

根据我的阅读,使用BSON的全部目的是因为它对CPU的负担更少,并且编码/处理速度更快.

From what I am reading, the entire point of using BSON is because it is less taxing on the CPU and faster to encode/process.

但是,我对Node.js进行了一些测试,并使用本机JSON方法打击了 BSON 脱离水面.一些测试表明JSON的速度提高了大约3到5倍. (使用更多数据类型时大约为6到8.)

But, I did some tests with Node.js and using a native JSON approach blows BSON out of the water. Some tests are showing JSON is around 3 to 5 times faster. (And around 6 to 8 when using more data types.)

基准代码:

var bson = require('bson');
var BSON = new bson.BSONPure.BSON();

var os = require('os');

console.log(" OS: " + os.type() + " " + os.release() + " (" + os.arch() + ")");
console.log("RAM: " + os.totalmem() / 1048576 + " MB (total), " + os.freemem() / 1048576 + " MB (free)");
console.log("CPU: " + os.cpus()[0].speed + " MHz " + os.cpus()[0].model);

for (var r = 1; r < 4; r++) {
    console.log("\nRun #" + r + ":");
    var obj = {
        'abcdef': 1,
        'qqq': 13,
        '19': [1, 2, 3, 4]
    };

    var start = Date.now();
    for (var i = 0; i < 500000; i++) {
        JSON.parse(JSON.stringify(obj));
    }
    var stop = Date.now();
    console.log("\t      JSON: " + (stop - start) + " ms");

    start = Date.now();
    for (var i = 0; i < 500000; i++) {
        BSON.deserialize(BSON.serialize(obj));
    }
    stop = Date.now();
    console.log("\t      Bson: " + (stop - start) + " ms");
}

结果:

OS: Windows_NT 6.1.7601 (x64)
RAM: 8174.1171875 MB (total), 5105.03515625 MB (free)
CPU: 3515 MHz AMD FX(tm)-6300 Six-Core Processor

Run #1:
              JSON: 1820 ms
              Bson: 8639 ms

Run #2:
              JSON: 1890 ms
              Bson: 8627 ms

Run #3:
              JSON: 1882 ms
              Bson: 8692 ms

话虽如此,我正在寻找一种通过websocket发送和接收数据的二进制方法. BSON可以完美地做到这一点,但是,当查看基准测试结果时,当序列化/反序列化对象花费更长的时间时,BSON如何减少对CPU的负担?

With that said, I am looking for a binary approach to send and receive data through websockets. And BSON does this perfectly, but, when looking at the benchmark results, how can BSON be less taxing on the CPU when it takes longer to serialize / deserialize objects?

BSON是否可以弥补使用的额外CPU使用量,因为使用基于文本的websocket不会转换为UTF-8吗?那会不会使这方面的表现达到水平?

Does BSON make up for the extra CPU usage it utilizes since there will be no conversion to UTF-8 with text based websockets? Would that level out the performance in that regard?

@Joe Clay,这是仅stringifyserializing的结果:

@Joe Clay below, here is the results for stringify and serializing only:

Run #1:
              JSON: 922 ms
              Bson: 355 5ms

推荐答案

问题不应该是为什么JSON比BSON快?,但是为什么在node.js中JSON比BSON快? ?.

在大多数环境中,BSON,MessagePack或CBOR之类的二进制编码比文本JSON编码更容易编码.但是,javascript环境(如v8/node.js)已针对JSON处理进行了优化(因为它是javascript的子集). JSON解/编码可能直接在JS VM中以优化的方式在本地代码中实现.但是,javascript VM并未针对表示和操作字节数组(由BSON库使用)进行优化.节点的本机Buffer类型可能比纯JS数组更好,但是使用它(例如执行JS字符串(UTF16)-> JS中的UTF8字节解码)仍然比内置的JSON序列化慢.

In most environments binary encodings like BSON, MessagePack or CBOR would be easier to encode than the textual JSON encoding. However javascript environments (like v8/node.js) are heavily optimized for JSON handling (because it's a subset of javascript). JSON de/encoding is probably implemented there in native code in optimized fashion directly in the JS VM. The javascript VMs are however not that optimized for representing and manipulating byte arrays (which is used by a BSON library). Nodes native Buffer type might be better than a pure JS array, but working with it (and doing for example the JS string (UTF16) -> UTF8 byte decoding in JS) is still slower then the inbuilt JSON serialization.

在其他具有直接字节数组访问和utf8字符串类型的C ++语言中,结果可能完全不同.

In other languages like C++ with direct byte array access and utf8 string types the results might be completely different.

这篇关于为什么在node.js中JSON比BSON更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆