PDA

View Full Version : Overhead of "unlistened" Events?



IQAndreas
December 17th, 2009, 06:02 PM
I'm wanting to override most properties (at least the display ones such as x, y, z, scale, alpha etc) of a class. Whenever one of those values changes, they will dispatch a custom event.

Especially with hundreds of items at once, this is likely to affect performance, especially on items that are constantly moving.

Will Events that are never listened to affect the performance anything? Is it mainly all the different listener functions that are what slow down?


Would this make any difference performance wise? (Mainly referring to the "hasEventListener" check)

public override function set x(new_x:Number)
{
if (this.hasEventListener(ChangeEvent.X))
{
//Check if the event was canceled
if (this.dispatchEvent(new ChangeEvent(ChangeEvent.X, new_x, _x)))
{
_x = new_x;
}
else
{
//Cancelled. Do nothing.
}
}
else
{
_x = new_x;
}
}
I'm guessing at least there might be a slight improvement, since it doesn't need to construct a new "Event" unless neede

wvxvw
December 17th, 2009, 06:13 PM
Hi.
There's a "hot" discussion here:
http://bugs.adobe.com/jira/browse/SDK-24249

What I usually do is like this:


//------------------------------------
// Public property x
//------------------------------------

[Bindable("xChanged")]

/**
* ...
* This property can be used as the source for data binding.
* When this property is modified, it dispatches the <code>xChange</code> event.
*/
public override function get x():Number { return _transformMatrix.tx; }

public override function set x(value:Number):void
{
if (_transformMatrix.tx == value) return;
_transformMatrix.tx = value;
this.invalidate("_transformMatrix", _transformMatrix, true);
if (super.hasEventListener(EventGenerator.getEventTyp e("x")))
super.dispatchEvent(EventGenerator.getEvent());
}


And my EventGenerator looks like this:


package org.wvxvws.binding
{
//{ imports
import flash.events.Event;
//}

/**
* EventGenerator class.
* @author wvxvw
* @langVersion 3.0
* @playerVersion 10.0.32
*/
public class EventGenerator
{
//--------------------------------------------------------------------------
//
// Private properties
//
//--------------------------------------------------------------------------

private static const _eventPool:Object = { };
private static const _changed:String = "Changed";
private static var _type:String;

//--------------------------------------------------------------------------
//
// Cunstructor
//
//--------------------------------------------------------------------------

public function EventGenerator() { super(); }

//--------------------------------------------------------------------------
//
// Public methods
//
//--------------------------------------------------------------------------

public static function getEventType(type:String):String
{
_type = type;
return type + _changed;
}

public static function getEvent():Event
{
if (!_eventPool[_type]) _eventPool[_type] = new Event(_type + _changed);
return _eventPool[_type];
}
}
}

But, I find that I personally don't use binding or, almost never do... I just don't know why I'm doing that :) to be honest.