phaser icon indicating copy to clipboard operation
phaser copied to clipboard

Improve visible children performance

Open JernejHabjan opened this issue 3 years ago • 3 comments

This PR

  • Improves performance

using native filter instead of manually looping and appending children to array

JernejHabjan avatar Sep 08 '22 17:09 JernejHabjan

I'm dubious that this really will improve performance as it does the same thing under the hood anyway - even so, it's less code, so I'm happy to merge it if you can fix the linting errors, please.

photonstorm avatar Sep 08 '22 20:09 photonstorm

Before: image After: image

Code snippet comparing the 2 functions:


dataArr = [];
items =["qqqqwwwweeee", "rrrrrrrzzzz"]
for(var i = 0; i < 3000; i++) {
  // push random data
  var item = items[Math.floor(Math.random()*items.length)];
  dataArr.push({
    attr1: "eqwrwqrwqewq eqwee",
    attr2: "ewqeqw ewqewq ewq",
    attr3: "ewqrqwrtqwewq ",
    attr4: 12321,
    attr5: item
  });
}

function start() {
  startTime = new Date();
}

function end(fnName) {
  endTime = new Date();
  var timeDiff = endTime - startTime; //in ms
  console.log(fnName + " " + timeDiff + " MS");
}

var repeatIterations = 1000000;

function startWithFilter() {
  start();
  for (var i = 0; i < repeatIterations; i++) {
    recordCollection = dataArr.filter((item) => item.attr5 === "qqqqwwwweeee");
  }
  end("filter fn");
}

function startWithForLoop() {
  start();
  for (var i = 0; i < repeatIterations; i++) {
    recordCollection = [];
    for (var j = 0, n = dataArr.length; j < n; j++) {
      var item = dataArr[j];
      if (item.attr5 === "qqqqwwwweeee") {
        recordCollection.push(item);
      }
    }
  }
  end("for loop fn");
}

startWithFilter();
startWithForLoop();

JernejHabjan avatar Sep 11 '22 08:09 JernejHabjan

@photonstorm Please review

JernejHabjan avatar Sep 11 '22 08:09 JernejHabjan

👍

photonstorm avatar Sep 29 '22 13:09 photonstorm