How to optimize a set to a buffer array?

  • 0
    Good afternoon!
    Have a question about code optimization.

    The goal is to understand how to reduce the amount of code in Example 1, without significantly affecting performance.

    Branchmark: https://jsben.ch/OsKbY
    Setup:
    let data1={test:new Float32Array(50000)}
    let data2={test:new Float32Array(50000)}
    
    
    function setBuffer(name, figure_num, data_p){
    
      let num = 5 * figure_num
      let buffer_array = data1[name]
      for (let i = 0, l = data_p.length; i < l; i++) {
        buffer_array[num + i] = data_p[i];
      }
    }


    Example 1:
    let pt1=[1,2]
        let pt2=[3,4]
        let status=5
        for(let i=0;i<10000;i++){
          let buffer_array = data2['test']
          let num = 5*i;
          buffer_array[num+0] = pt1[0];
          buffer_array[num+1] = pt1[1];
          buffer_array[num+2] = pt2[0];
          buffer_array[num+3] = pt2[1];
          buffer_array[num+4] = status;
    
        }


    Example 2:
    let pt1=[1,2]
    let pt2=[3,4]
    let status=5
    
    for(let i=0;i<10000;i++){
      setBuffer('test', i, [pt1[0],pt1[1],pt2[0],pt2[1],status])
    }


    JavaScript Anonymous, Sep 3, 2019

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!