A
A
Anton V.2020-10-22 19:58:43
JavaScript
Anton V., 2020-10-22 19:58:43

How to optimize a set into a buffer array?

Good afternoon!
I have a question about code optimization.

The goal is to understand how to reduce the amount of code in Example 1 without significantly reducing 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])
}


5f91b96c80258756211465.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question