简体   繁体   中英

Handling WebSocket Data Faster

So I get my data (in JSON format) from a website's websocket. It works, but the problem is that handling this data is not as time efficient as I wish it to be (every milisecond matters). Currently my handler looks like this:

var events_channel = pusher.subscribe('changes');
const eventsQueue = [];

events_channel.bind('channel1', function(data)
{
  eventsQueue.push(data);
  handleNewEvent();
});
events_channel.bind('channel2', function(data)
{
  eventsQueue.push(data);
  handleNewEvent();
});


let processingEvent = false;
function handleNewEvent() 
{
    if(processingEvent){return;}
    processingEvent = true;
    const eventData = eventsQueue.shift();
    if(!eventData){processingEvent = false; return;}

    //Parse the data and do some other stuff with it

    processingEvent = false;
    handleNewEvent();
    return;
}

I have no say over how the websocket works on the server side, so I'm wondering if there is a way to save an extra milisecond or two, or if this is basically it in regards to what I can do efficiency wise.

First of all, I do not see a need to call 'handleNewEvent' recursively, this might just end up biting you.

A few things come to mind here:

  • Shifting arrays performs much worse than just using .pop() . This might be just fine if you do not experience high load or too many
    messages per second, but might screw you over. Consider writing a custom queue that is tailored to your needs.

  • In case you can assume that the messages will always have exactly the same format, you can write a custom parser for your messages. Just use fixed offsets to index certain parts. This should out-perform JSON.parse, but beware that you need to be absolutely sure that
    format will never change. From my experience, JSON.parse took ~2
    microseconds, which is very very hard to beat, even with indexing directly into a buffer/string.

  • Try to not allocate new objects on your hot path (pre-allocate what
    you need) and do not change the fields of those objects afterwards
    (This will trigger new hidden classes which can degrade
    performance).

  • Lastly, V8 can optimize functions which always take the same type of input. It is sometimes preferable to write specialized functions for operating on different types rather than using polymorphic
    functions . V8 will also start to inline wherever possible.

After all, remember premature optimization. Write your code, benchmark, improve the slow parts and repeat. And if you really care about performance, you might be better off dropping into a language that gives you finer control. In node you will end up fighting the GC and V8 at some point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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