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

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

问题描述

阅读这篇后,有一句引人入胜:

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

BSON 还被设计为快速编码和解码.例如,整数存储为 32(或 64)位整数,因此它们不需要与文本进行解析.对于小整数,这比 JSON 使用更多的空间,但解析速度要快得多.

BSON is also designed to be fast to encode and decode. For example, integers are stored as 32 (or 64) bit integers, so they don't need to be parsed to and from text. This uses more space than JSON for small integers, but is much faster to parse.

从我读到的内容来看,使用 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("
Run #" + 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("	      JSON: " + (stop - start) + " ms");

    start = Date.now();
    for (var i = 0; i < 500000; i++) {
        BSON.deserialize(BSON.serialize(obj));
    }
    stop = Date.now();
    console.log("	      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

话虽如此,我正在寻找一种通过 websockets 发送和接收数据的二进制方法.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 使用率,因为使用 基于文本的 websockets 不会转换为 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 库使用)进行优化.节点本机缓冲区类型可能比纯 JS 数组更好,但使用它(例如在 JS 中执行 JS 字符串 (UTF16) -> 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.

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

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