Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance benchmarks between arrays and Object.keys - create, access, delete. #971

Closed
JSideris opened this issue Apr 15, 2020 · 0 comments

Comments

@JSideris
Copy link
Contributor

JSideris commented Apr 15, 2020

Code

Setup:

var collection = {};
var array = [];
var n = 100;
var MyObj = function(id){
    this.id = id;
    this.y = 0;
}

MyObj.prototype.use = function(x){
    this.y = 2*x;
}

Creation:

for(let i = 0; i < n; i++){
    collection[i] = new MyObj(i);
}
for(let i = 0; i < n; i++){
    array.push(new MyObj(i));
}

Linear Access:

let i = 0;
Object.keys(collection).forEach(k=>{
    i++;
    collection[k].use(i);
}); 
let K = Object.keys(collection);
let i = 0; 
for(let k in K){
    i++;
    collection[k].use(i);
}
for(let i = 0; i < array.length; i++){
  array[i].use(i);
}

Random Access:

/*setup*/ var rand = Math.floor(Math.random() * n);
collection[rand].use(123);
/*setup*/ var rand = Math.floor(Math.random() * n);
for(let i = 0; i < array.length; i++){
    let a = array[i];
    if(a.id == rand){
        a.use(123);
        break;
    }
}

Deleting:

let i = 0;
Object.keys(collection).forEach(k=>{
    delete collection[k];
}); 
while(array.length > 0){
    array.splice(0, 1);
}

Results

Creating - Objects vs Arrays
For n=1000:
image

Linear Access - Objects vs Arrays
image

(BONUS) Linear Access - Object.keys.forEach vs Iterating Over Object.keys In a Regular For Loop
image

Random Access - Objects by ID vs Array search - n=100
image

Random Access - Objects vs Array search - n=1000
image

Random Access - Objects vs Array search - n=10000
image

Deleting - Objects (delete) vs Arrays (splice)
image

Conclusion

In the game, creating and deleting are relatively rare. Objects are only created once, but updated many, many times over the duration of their life. So the create/delete metrics are far less relevant. The vast majority of updates require linear access, for instance, updating objects once per frame.

The most surprising result here is that For n <= 100, arrays just as performant as objects for random access, even though the time complexity to find one is on the order of n.

Wherever linear updates are required (draw/fixed updates), use arrays. Iterate in reverse order, and if an item is dead, splice it from the array. To handle random updates (from the server, etc) have a repository of all game objects in an object. When the game object needs to be deleted, call it's delete function, which should delete it from the global container object, and set a flag indicating that it should be spliced from any arrays it's in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant