简体   繁体   English

Node.js写入数组而不会阻塞

[英]Node.js writing to array without blocking

I am new to Node would appreciate some advice on writing to an array. 我是Node的新手,不胜感激有关写入数组的一些建议。 I have high frequency real-time data coming in, and each tick will be written to a single array. 我有高频实时数据输入,每个刻度都将写入单个数组。

Would it be beneficial to do this in an asynchronous way? 以异步方式执行此操作是否有益? If so, how can this be done? 如果是这样,该怎么办?

Here is a trimmed down snippet of my code. 这是我的代码的精简片段。 The "filter" function is called when a new tick is received. 收到新的报价后,将调用“过滤器”功能。 The "storeWindowData" function then saves to the array. 然后,“ storeWindowData”函数保存到数组。 I presume this could be blocking under high load? 我认为这在高负载下可能会阻塞?

Appreciate any comments. 感谢任何评论。

Regards, Ben. 问候,本。

var window_data = [];
module.exports = {

filter: function (data) {

    this.storeWindowData(timestamp, ticker, content);

    }
},

storeWindowData: function(timestamp, ticker, content){
    // Check if we have seen this ticker before for this minute
    if(window_data[timestamp] !== undefined && window_data[timestamp][0] !== undefined && window_data[timestamp][0] === ticker){
        window_data[timestamp][1] =  window_data[timestamp][1] + ',' + content;
    } else {
        window_data[timestamp] = [ticker, content];
    }
}     

}; };

An array is an in memory primitive (well, built in type, not particularly primitive). 数组是内存中的原语(嗯,是内置类型,不是特别原始)。

How exactly could you, at a JavaScript level, async write to it? 在JavaScript级别上,您究竟如何异步写入?

Where would it be stored temporarily while waiting to be written? 等待写入时将其暂时存储在哪里? A second array? 第二个数组? (See the problem? Writing to that array would cause two array writes, which would be twice as slow.) (看到问题吗?写入该阵列将导致两次写入阵列,这将是速度的两倍。)

You're going to have to identify a different bottleneck, or if this really is the problem (which I'm very, very sure that it's not), you have either too weak of a machine handling this, or you have an insanely high amount of data coming in. 您将不得不确定一个不同的瓶颈,或者如果这确实是问题所在(我非常非常确定这不是问题),或者您的计算机处理此问题的能力太弱,或者您的数据异常高传入的数据量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM