• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Optimization Question: 1 handler for multiple events or 1 handler per event?

Guest
Apr 21, 2015 Apr 21, 2015

Copy link to clipboard

Copied

Something I'm having trouble deciding, simply because I'm not sure which would be more effective.

Example: I have a couple menus, each with a handful of icons to click on (lets say a total of 10 objects that can be clicked on).

Would it be better to have 1 stage.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler); with 10 if(e.target.name == "nameX") that gets called on every click.

Or would it be better to have 10 separate objectX.addEventListener(MouseEvent.MOUSE_DOWN, clickHandlerX); for each menu item that can be clicked on?

I guess my confusion comes from not knowing what a listener is doing exactly, is it using any resources watching for an event?

(added)

After typing all that out, would a correct comparison be closer to an event listener is nothing but a way of calling a function and otherwise isn't using resources?

My question still remains, but I'm leaning towards multiple listeners and handlers.

(added 2)

Sorry my brain scrambles, I'm not good at explaining things.  I think this is a bit clearer.

For 1 menu I have 1 this.addEventListener, and in the handler it has multiple if (menu_itemX.name == "nameX").
Is it better to have 1 eventListener in this situation or would it be better to have multiple menu_itemX.addEventListener and seperate handlers for each action?

(followup question)

I've been doing addEventListener when the menu opens and removeEventListener when the menu closes.  If event listeners do use "no" resources unless their event is triggered is this a bad practice for simple click and mouseover and mouseout events?  Should I just leave the event listeners there all the time?

(code example)

If this makes it any clearer, which of these would work best?

Version 1:

this.menu1.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler1);

this.menu2.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler2);

this.menu3.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler3);

this.menu4.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler4);

this.menu5.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler5);

function clickHandler1(e:Event):void{}

function clickHandler2(e:Event):void{}

function clickHandler3(e:Event):void{}

function clickHandler4(e:Event):void{}

function clickHandler5(e:Event):void{}

version 2:

this.addEventListener(MouseEvent.MOUSE_DOWN, clickHandler);

function clickhandler(e:Event):void{

if(e.target.name == "menu1"){}

else if(e.target.name == "menu2"){}

else if(e.target.name == "menu3"){}

else if(e.target.name == "menu4"){}

else if(e.target.name == "menu5"){}

}

TOPICS
ActionScript

Views

335

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 22, 2015 Apr 22, 2015

leave them alone as long as your menu works as expected while they're added.

leaving the listener occupies memory.  removing it frees that memory.

but freeing memory and adding to memory uses system resources, especially when flash gc's to free memory.  it's inefficient to repeatedly gc objects that are going to be re-added to memory later.

Votes

Translate

Translate
Community Expert ,
Apr 22, 2015 Apr 22, 2015

Copy link to clipboard

Copied

there's no significant difference and whatever difference there is varies with flash player version.

use coding that's clearest/easiest to understand.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 22, 2015 Apr 22, 2015

Copy link to clipboard

Copied

So should i use addEventListner to each menu item when the menu is open and removeEventListener when the menu closes or should I just add them at loading and leave the event listeners there?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 22, 2015 Apr 22, 2015

Copy link to clipboard

Copied

if the event listeners are never needed again, remove them.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 22, 2015 Apr 22, 2015

Copy link to clipboard

Copied

I assume that's a reverse-answer to my question, the only time you should remove an event listener is if it will never be needed again?

For my example, those event listeners are only used while the menu is open.  Should I add/remove as the menu is open/closed or should I add and leave them active because they will potentially be needed if the menu gets opened?

(added)
I am focused on efficiency.  I can make the code look pretty whatever is happening.  I would prefer to stick with add/remove, but that's coming from the place of "if the event can't happen there isn't a reason to use resources listening for it". However if the listener really uses "no" resources then does adding and removing the listener use more resources then just leaving the listener alive?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 22, 2015 Apr 22, 2015

Copy link to clipboard

Copied

LATEST

leave them alone as long as your menu works as expected while they're added.

leaving the listener occupies memory.  removing it frees that memory.

but freeing memory and adding to memory uses system resources, especially when flash gc's to free memory.  it's inefficient to repeatedly gc objects that are going to be re-added to memory later.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines