【问题标题】:Why does sending a Date Object as a message from a worker thread convert it to a string?为什么从工作线程发送日期对象作为消息将其转换为字符串?
【发布时间】:2019-07-26 22:52:44
【问题描述】:

我正在尝试跨多个 CPU 运行一些代码。每个 cpu 都被分配了一些任务,然后它们将结果返回给主线程,主线程将所有内容聚合在一起。

我不明白的是,每当我发送包含日期对象的消息时,该日期对象都会转换为字符串。必须在工作线程和主线程中解析这些日期似乎很荒谬。我正在处理很多约会,因此这会对性能产生巨大影响。

我能做些什么来解决这个问题吗?我正在使用节点版本 v10.13.0。

const cluster = require('cluster');

if (cluster.isMaster) {
  // init cluster
  require('os').cpus().forEach(() => {
    cluster.fork();
  });

  // add eventlisteners
  Object.values(cluster.workers).forEach(worker => {
    worker.on('message', message => {
      console.log(typeof message); // string
    });
  });
} else {
  process.send(new Date());
}

【问题讨论】:

    标签: node.js node-cluster


    【解决方案1】:

    根据'message'事件的documentation

    消息经过序列化和解析。

    Date 对象的序列化是一个字符串:

    // serialize and rebuilds an object: { test: "2019-03-05T16:20:17.863Z" }
    JSON.parse(JSON.stringify({test: new Date()}));
    

    所以,不,没有解决方法:每个进程(worker 和 master 一样)都有自己的环境(即它自己的存储对象的空间),因此您不能在不同的环境之间共享引用。举例说明:

    const input = {some: 'thing'};
    const output = JSON.parse(JSON.stringify(input));
    console.log('are input and output the same object?', input === output); // false
    

    如果您担心这方面的性能,不妨重新考虑您的架构,以便工作人员无需通过通道发送那么多日期。

    附带说明一下,使用Date 的内部时间戳代替默认时间字符串可能会提高性能:

    const t0 = new Date();
    for (let i = 0; i < 1000000; i++) {
        const tmp = new Date();
        // this took ~6.9s on JSFiddle
        new Date(JSON.parse(JSON.stringify(tmp)));
        // this took ~2.8s on JSFiddle
        new Date(JSON.parse(JSON.stringify(+tmp)));
    }
    const t1 = new Date();
    console.log('time spent', t1 - t0);
    

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 2020-05-21
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多