phaser
phaser copied to clipboard
Improve visible children performance
This PR
- Improves performance
using native filter instead of manually looping and appending children to array
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.
Before:
After:

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();
@photonstorm Please review
👍