schteppe / p2.js

JavaScript 2D physics library
Other
2.63k stars 329 forks source link

Fixed inefficient way of resetting an object #321

Open issy123 opened 6 years ago

issy123 commented 6 years ago

Looping through all object keys to delete them is very inefficient, rather remove the reference to the object and create a new object. The garbage collector is efficient enough to handle this.

englercj commented 6 years ago

Just for some historical context, the reason I did it this way was because there actually were GC pause issues with allocating a new object. reset() is a hot function that is run multiple times each frame, which if it allocates generated a lot of garbage very quickly.

The compromise was to be slightly slower to run, but consistent. Rather than sometimes being faster, but spiking on GC.

issy123 commented 6 years ago

Well it is significant faster I my experience with node v6+.

What would you suggest?

englercj commented 6 years ago

I always suggest measuring it, a benchmark suite that proves one thing or another is faster is always great.

Nek- commented 6 years ago

We need a benchmark for many browsers and different versions of nodejs for this one :/ .