Skip to content

Microbenchmark to compare performance of arrays vs objects for vector math models

License

Notifications You must be signed in to change notification settings

anvaka/array-vs-object

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Array vs Object

When it comes to vector math in javascript it is usually done in a few ways. I was curious to see if there is any difference in the performance of these methods. Particularly for the case when array of vectors is required.

Let's consider possible options:

Option 1: Vector class

class Vector {
  constructor(x, y, z) {
    this.x = x;
    this.y = y;
    this.z = z;
  }
  // ...
}
let v = new Vector(0, 1, 2);

Option 2: Array of triplets

let v = [0, 1, 2]; // vector with three items

Option 3: Pre-allocated array

let buffer = new Float64Array(1000);

let vIndex = 0;
buffer[vIndex + 0] = 0;
buffer[vIndex + 1] = 1;
buffer[vIndex + 2] = 2;

The benchmark

Disclaimer: The benchmark has to be taken with grain of salt, as I'm not sure whether v8 optimizes it in any way under the hood.

With that disclaimer, the benchmark initializes 1,000 vectors with each approach, and then finds the sum of all vectors:

for(let i = 0; i < 1000; ++i) {
  objectVectors.push(new Vector(i, i, i))
}

let sum = new Vector(0, 0, 0);
for (let i = 0; i < bodyCount; ++i) {
  sum.add(vectors[i])
}
vectorSum = sum;

This method is adjusted for each method described above, and then we just measure how many times we are able to execute this operation per second.

Results

> node --version
v16.4.0

> node index.js
Compute sum with Vector(x, y, z) x 68,281 ops/sec ±2.32% (81 runs sampled)
Compute sum with array[x, y, z] x 45,295 ops/sec ±3.77% (80 runs sampled)
Compute sum with huge chunk x 72,070 ops/sec ±2.00% (81 runs sampled)
Fastest is Compute sum with huge chunk
Vector sum: 499500 499500 499500
Array sum: 499500 499500 499500
Chunk sum: 499500 499500 499500

Like mentioned above, take these numbers with grain of salt. I did n-body simulation tests with these approaches, and performance was almost identical:

> node nbodyTest.js 
Compute n-body with Vector(x, y, z) x 14.80 ops/sec ±2.74% (39 runs sampled)
Compute n-body with array[x, y, z] x 14.81 ops/sec ±3.17% (40 runs sampled)
Compute n-body with Float64Array(3) x 14.80 ops/sec ±2.26% (40 runs sampled)
Fastest is Compute n-body with Float64Array(3),Compute n-body with Vector(x, y, z),Compute n-body with array[x, y, z]
Object avg position length: 4.402166577294573e-15
Array avg position length: 4.402166577294573e-15
Float64Array avg position length: 4.402166577294573e-15

Other benchmarks

  • Set vs Object - what is faster: use object fields or Map/Set collections?
  • Iterator vs foreach - what is faster: use forEach, for, for .. of, [Symbol.Iterator](), or yield *?

Feedback

If you want to add other tests - please do so. Pull requests are very much welcomed!

License

MIT

About

Microbenchmark to compare performance of arrays vs objects for vector math models

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published