Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Seanny123 Aug 13 at 17:27

1 Answer 1

up vote 1 down vote accepted

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();
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.