Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I make this more efficient? Creating a buffer in Javascript with NodeJS, Socket.io
    text
    copied!<p>I'm getting a lot of data on my socket.io client side that may or may not be complete data and its also potentially BIG data, so I need to make it efficient. So I have tried my hand at creating a buffer and parsing that into my app. I know there are some streaming/buffering modules available, and I might consider using those if they achieve the goal of being more efficient. Looking forward to seeing your answers and possible arguments on how to best do this. </p> <p><em>Note, bandwidth is not my concern as much as how quickly the client side Javascript can render the data into a browser friendly format.</em></p> <p>Here is what I've got so far.</p> <pre><code>function extended_split(str, separator, max) { var out = [], index = 0, next; while (!max || out.length &lt; max - 1 ) { next = str.indexOf(separator, index); if (next === -1) { break; } out.push(str.substring(index, next)); index = next + separator.length; } out.push(str.substring(index)); return out; }; var buffer = ''; // data format "\nOP:ARGS:DATA(could be base64 or 'other' depending on OP)\0"; socket.on('ioSend', function(data) { data = String.fromCharCode.apply(null, new Uint16Array(data)); buffer = buffer + data; while(buffer.indexOf('\n') != -1 &amp;&amp; extended_split(buffer, '\n', 2)[1].indexOf('\0') != -1) { splitted = extended_split(extended_split(buffer, '\n', 2)[1], '\0', 2); parse = splitted[0]; buffer = splitted[1]; parse = parse.split(':'); // Do stuff with parse here } }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload