1
\$\begingroup\$

I have this code for parsing for parsing a list of floats of variable length.

on_message = function(event_data) {
 var data = event_data.split(" ").map(parseFloat)
 var size = data.shift() + 1;
 while (data.length >= size) {
 // put the data in a buffer
 // could be replace by console.log() for testing purposes
 // each data chunk needs to be pushed separately
 this.data_store.push(data.slice(0, size));
 data = data.slice(size);
 size = data.shift() + 1;
 }
 // start displaying stuff to the user ASAP
 this.schedule_update();
}

At this point, the data is formatted in the following manner when a single "chunk" is sent:

[length data_1 data_2 etc etc]

However, because the back-end that sends these updates can sometimes be faster than the front-end, multiple "chunks" can be sent at the same:

[length_1 data_1_1 data_1_2 etc_1 etc_1 length_2 data_2_1 data_2_2 etc_2]

As mentioned before, all values in event_data are floats, except for the length which is an int.

The format of the data packet is not set, although the fact that more than one "chunk" can be sent at a time and the chunks are different sizes cannot be changed. I would just like to be able to send a variable length array of floats to a buffer quickly and efficiently. Right now, this code is taking really long. What can I do to speed it up?

asked Aug 12, 2015 at 22:20
\$\endgroup\$
1
  • \$\begingroup\$ I'll leave this question in case other users have similar problems, however I've since realised that sending multiple chunks at the same time was an assumption and not a requirement, thus this question no longer has any personal relevance to me. \$\endgroup\$ Commented Aug 13, 2015 at 17:27

1 Answer 1

1
\$\begingroup\$

You can get some improvements by using JSON and moving the length increment to the back-end like so:

Nengo.SpaSimilarity.prototype.on_message = function(event) {
 var data = JSON.parse(event.data);
 var size = data.shift();
 while (data.length >= size) {
 this.data_store.push(data.slice(0, size));
 data = data.slice(size);
 size = data.shift();
 }
 this.schedule_update();
}
answered Aug 12, 2015 at 23:21
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.