voxel-mesher
voxel-mesher copied to clipboard
Constantly re-allocating array is much slower than appending
The code at mesh-plugin.js:101
currently reads:
// accumulate mesh vertices and uv
var model = this.meshCustomBlock(value,x,y,z);
vertices = vertices.concat(model.vertices);
uv = uv.concat(model.uv);
The use of concat
causes the allocation of new arrays (of ever increasing size), which is needlessly expensive. Appending in-place is much faster, e.g.
var model = this.meshCustomBlock(value,x,y,z);
for (var j = 0; j < model.vertices.length; j++) {
vertices.push(model.vertices[j]);
}
for (var k = 0; k < model.uv.length; k++) {
uv.push(model.uv[k]);
}
I'm rendering some maps using a chunk distance of 2-4, 32x32x32 chunks, and using custom block models for a significant fraction of the blocks in the scene. This change significantly improves performance for my application (reducing meshing time by as much as 10 seconds!)