Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast way to fill array with Objects

I have to fill an array with a lot of objects. My current Implementation is this:

let useVertices = [];
const len = this.indices.length;
for(let i = 0; i < len; i++){
    let index = this.indices[i]*3;
    useVertices.push(new THREE.Vector3(
        this.vertices[index],
        this.vertices[index+1], 
        this.vertices[index+2])
    );
}

this.indices is an Int32Array with a length of almost 4 million. this.verticesis a Float32Array with a length of about 650,000.

The implementation shown above takes between 500 and 800 ms.

The Browser I am using is CefSharp, because the website is run in a C# application.

Is it possible to increase the speed of this code?

like image 542
Plat00n Avatar asked Oct 15 '25 19:10

Plat00n


1 Answers

Gathering together what I think are the best suggestions from the comments (other than frame challenges) pending your testing them:

  • Ouroborus pointed out that since you have 4M indices but only 650k vertices, you'll often be creating equivalent THREE.Vector3 instances. We can reuse instances instead, remembering previous ones based on index.
  • Gabriele Petrioli pointed out that you can use assignment rather than push, saving a method call: useVertices[i] = ___
  • You can pre-allocate the array using new Array(len)
  • Avoid unnecessary property lookups by caching indices, vertices, and THREE.Vector3
  • Use const for useVertices if you can
const { indices, vertices } = this;
const V = THREE.Vector3;
const len = indices.length;
const useVertices = new Array(len);
const vectors = new Map(); // For reusing vectors
for (let i = 0; i < len; i++) {
    let index = indices[i] * 3;
    let vector = vectors.get(index);
    if (!vector) {
        vector = new V(
            vertices[index],
            vertices[index + 1],
            vertices[index + 2]
        );
        vectors.set(index, vector);
    }
    useVertices[i] = vector;
}

(Note: Plat00n tried using an array instead of a Map above, but even with the get and set method calls, the Map was faster.)

like image 108
9 revs, 3 users 98%T.J. Crowder Avatar answered Oct 18 '25 12:10

9 revs, 3 users 98%T.J. Crowder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!