phaserjs / phaserjs/phaser

Performance Bottleneck with Group.get() Method in Phaser

Open
#7,140 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
40.3k
Forks
7.2k
PR merge metrics
No merged PRs in 30d

Description

Problem Description

I'm experiencing significant performance issues with the Group.get() method in Phaser. The method takes an unusually long time to execute, especially when the group contains a large number of objects. This significantly impacts the performance of my application.

Code Snippets

main.js (Relevant Part)
const seedItems = scene.add.group({
    classType: SeedItem,
    maxSize: -1,
    runChildUpdate: false
});

this.fillItems = (itemClass, itemDataList, cols = 6, itemWidth = 181, itemHeight = 201) => {
    const panel = scrollList.getElement('panel');
    const line = Math.ceil(itemDataList.length / cols);

    itemDataList.forEach((data, index) => {
        const row = Math.floor(index / cols);
        const col = index % cols;
        const x = col * (itemWidth + 20) + 160;
        const y = row * (itemHeight + 30) + 110;
        let start = performance.now();
        const seedItem = seedItems.get();
        console.log("cost", performance.now() - start);
        if (seedItem) {
            seedItem.updateData(x, y, data);
            seedItem.setActive(true);
            seedItem.setVisible(true);
            panel.add(seedItem);
        }
     
    });

    panel.setSize(GameConfig.Screen.width, (itemHeight + 30) * line + 30);
    scrollList.layout();
};
SeedItem.js (Simplified and Sanitized)
export default class SeedItem extends Phaser.GameObjects.Container {
    constructor(scene, x, y, children) {
        super(scene, x, y, children);
        // UI elements initialization
        this.bg = scene.add.image(0, 0, "item_atlas", "item_bg.png").setInteractive({ useHandCursor: true });
        this.icon = scene.add.image(0, 0, "common_atlas", "icon_seed_default.png");
        this.levelText = scene.add.text(-70, -95, "", { fontFamily: "Arial", fontSize: "24px", fill: "#ffffff" });
        this.countText = scene.add.text(45, 35, "", { fontFamily: "Arial", fontSize: "24px", fill: "#385bb6" });
        this.nameText = scene.add.text(0, 85, "", { fontFamily: "Arial", fontSize: "22px", fill: "#fffffb" }).setOrigin(0.5, 0.5);
        this.notBuyBg = scene.add.image(0, 0, "item_atlas", "item_not_available.png").setVisible(false);

        // Event listeners
        this.bg
            .on("pointerdown", (pointer) => { /* ... */ })
            .on("pointermove", (pointer) => { /* ... */ })
            .on("pointerup", (pointer) => { /* ... */ });
    }

    updateData(x, y, data) {
        this.setPosition(x, y);
        const { level, count, name, type } = data;
        this.levelText.setText(`Lv.${level}`);
        this.countText.setText(count);
        this.nameText.setText(name);
    }
}

Steps to Reproduce

  1. Create a group with maxSize: -1.
  2. Repeatedly call group.get() in a loop (e.g., for populating a grid).
  3. Observe the execution time of each get() call.

Expected Behavior

The get() method should return a pooled object quickly, even for large groups.

Possible Workarounds

  • Pre-warm the pool by pre-creating objects.
  • Limit the group size if possible.

Could you investigate this issue or suggest optimizations? Thanks!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the repeated Group.get() calls from main.js with a large group, then inspect the Phaser Group.get() implementation and compare timing with the SeedItem setup in SeedItem.js. Determine whether the cost is in object pooling or construction, and define completion as a demonstrated performance improvement that preserves the expected pooled-object behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
game-dev, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.