Performance creating vs cloning entities

Hi, I would like to know if there’s any difference in performance between using new Entity() several times to create entities or just creating a single one and then cloning the others using entity.clone.

I’m running into some CPU usage troubles on the server side (ported the PlayCanvas engine into a headless nodejs app which is running it to help me run the game logic on the server), and I am just looking at what’s the best practices to save CPU usage.

Just to clarify: My current problem is that the CPU usage on the server never goes down, no matter if a player disconnects or anything, it only goes up slowly as new players spawn and interact with the world (creating new entities and destroying others), until it reaches 100% and the application doesn’t respond.

This sounds like something is actually not being destroyed or memory collected which leads to something like so.
Cloning does new internally, but does inherits stuff a bit more efficiently.

Best practice is actually avoid re-creating objects all the time, and to make a pull of them and reuse them.
Something like this:

var template = app.root.findByName('entityTemplate');
var pool = [ ];

var create = function() {
    var item;
    if (pool.length) {
        item = pool.shift();
    } else {
        item = template.clone();
    return item;

var delete = function(item) {

So it gives you ability to allocate only certain number of entities, and instead of destroying and creating again, which is indeed does a lot. Instead it will remove it from parent and add back to pool. Then when you need it again, it will try to get one from pool or make a new clone.

This is called “object pooling” and is efficient way to minimize allocations and cpu usage associated with it.

But as mentioned, your problem might be related that you somewhere are increasing some arrays, and not making them smaller. I assume you are using node.js on server and you can actually run it with an inspector (like Dev Tools). Using this inspector you can profile JS. So get your server loaded to stage it uses a lot of CPU and then do some profiling, it will highlight place where it spends most of the time in JS, and investigate that area :slight_smile:

Yup I figured I should use object pools on the server too (already doing it client side).

As you mentioned, I do have actually 3 arrays that never stop growing, one stores players, and the other stores the actors related to them, I never remove any item from the array because I thought iterating an array is far more expensive than directly doing something like:


So if 100 players joined the server during the day, the players array will contain 100 items all day, I just set them null and don’t send any data for the null players.

Am I doing it very wrong, and is it these arrays which are consuming the CPU endlessly?

Also, how to profile with Dev Tools? I am using nodejs. I’ve just been frustrated looking at the CPU usage with top and trying to “fix” bits of code and see if the cpu usage was lower or something.

If you do set data to null and do not keep any references to it around, then it should be garbage collected, but you really need to ensure you do not store any reference to that data. As if you do, then it won’t be GC’ed, and will consume memory and potentially CPU.

So best thing to do, as mentioned plug Inspector to it, and look at CPU usage using Performance tab in Chrome.
Here is good blog post about inspector and node:

I don’t know, I tried splicing arrays and still won’t save any CPU… it just goes up slowly and never goes down, sigh.

Also the inspect thing doesn’t seem to work! :frowning:

I’m really missing something, this doesn’t make any sense.

I don’t have experience of node.js but one thing you can do to help narrow down the area where the CPU spike might be coming from is to put in your own timings on some of the higher level logic blocks.


Something.prototype.update = function (dt) {

  printTimer("Player Updates");


You get the idea. This will at least help narrow down which area is causing the CPU to go up and then you can keep narrowing down which subsystem in your code is causing the issue (hopefully!)

Ideally, you should get the Chrome Tools working as it will be much quicker to isolate the offending code.