1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to smoothly shuffle very large array of data source into data processing function

Discussion in 'JavaScript' started by ketting00, Sep 25, 2014.

  1. #1
    Hi guys,
    I've a data processing function similar to structure below. The processing function is slowing down when I change data source to another one. It's audio data processing function.

    button1.addEventListener('click', function() {
       ...
       var arr = [],
         buffer = e.outputBuffer.getChannelData(0);
       for (var i = 0; i < arr.length; i++) {
         arr[i] = buffer[i];
         processing(arr[i]);
       }
    }); // source one
    
    button2.addEventListener('click', function() {
       ...
       var arr = [],
         buffer = e.outputBuffer.getChannelData(0);
       for (var i = 0; i < arr.length; i++) {
         arr[i] = buffer[i];
         processing(arr[i]);
       }   
    }); // source two
    
    function processing(j) {
       var this.j = j
        , node = context.createBufferSource()
        , buffer = context.createBuffer(1, 4096, context.sampleRate)
        , data = buffer.getChannelData(0);
       for (var i = 0; i < 4096; i++) {
         data[i] = j;
         ...
       }
       ...
    }
    Code (markup):
    Everything is working fine with one data source (one song), but if you shuffle data source between two songs or add more data sources by clicking or anything the music quality becomes choppy and bad and worst and crash my browser eventually.

    I heard the for loop is bad. Does it has anything to do with this.

    Any idea how can I manage these kind of array source is greatly appreciated.

    Thank you,
     
    ketting00, Sep 25, 2014 IP
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #2
    I don't understand other parts, but this one perhaps:
    button1.addEventListener('click', function() {
       ...
       var arr = [],
         buffer = e.outputBuffer.getChannelData(0);
       for (var i = 0; i < arr.length; i++) {...
    Code (JavaScript):
    A zero-length, un-initialized arr will always be created at any clicks on button1,
    so that the for() loop will never actually be executed ??
     
    hdewantara, Sep 25, 2014 IP
  3. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #3
    Thanks for help.

    I can sense what you mean. But I don't understand why the for() loop will never actually be executed.
    How can I solve this.
    According to the internet, they said var arr = []; is better than var arr = new Array(4096);
    So I follow those instructions.
     
    ketting00, Sep 25, 2014 IP
  4. hdewantara

    hdewantara Well-Known Member

    Messages:
    536
    Likes Received:
    47
    Best Answers:
    25
    Trophy Points:
    155
    #4
    Maybe they mean var arr = [4096] & not just var arr = [] ? The latter means creating an empty array (a.k.a. array with zero-length) & since your for() loop depends on this length... you know what I mean.
     
    hdewantara, Sep 25, 2014 IP
  5. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #5
    Thanks for the advice, I'll test the code accordingly...
     
    ketting00, Sep 25, 2014 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Is there some reason you are making an extra copy of the array values?!? Is there some reason you couldn't just pass your entire array to your "processing" function?

    Seems like you're doing a lot of array stuff for nothing -- but it's hard to say without seeing the rest of the code or having more info on the data source that's being processed.
     
    deathshadow, Oct 6, 2014 IP
  7. seductiveapps.com

    seductiveapps.com Active Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #7
    be careful when working with very large arrays in the browser --> all browsers still to this day have very crappy memory cleanup routines leading to "memory leaks", meaning that you see the problems reported by the OP.

    make as little copies as possible, keep an eye on (windows:ctrl+shift+esc --> advanced) the meters for memory consumption, and if you see the browser eating up more and more memory, you know you got a memory leak.

    would help if you'd all read the google hits for "memory leak browser" :)
     
    seductiveapps.com, Oct 25, 2014 IP
  8. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #8
    Thank you @seductiveapps.com,

    That was what I suspected. My web app plays animation at 60 frames a second and sound frame at much higher rate. The higher resolution the choppier quality.
     
    ketting00, Oct 26, 2014 IP
  9. seductiveapps.com

    seductiveapps.com Active Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #9
    Then you need to be super careful about your variable assignments.

    Basically, all var = someDataStoredInBrowser.some.sub.variable.path statements are Bad. Start there and your problems should appear much later.
     
    seductiveapps.com, Oct 26, 2014 IP
  10. ketting00

    ketting00 Well-Known Member

    Messages:
    772
    Likes Received:
    27
    Best Answers:
    3
    Trophy Points:
    128
    #10
    Thanks for suggestion,
    Sound like you're familiar with this stuff. I only see Google engineers working on this.
     
    ketting00, Oct 26, 2014 IP
  11. seductiveapps.com

    seductiveapps.com Active Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #11
    I don't do audio processing or visuals processing, I work with very large JSON databases sometimes, which suffer from the same problem as soon as you try to visualize them in the browser ;)
     
    seductiveapps.com, Oct 26, 2014 IP