May be nobody need to create types interit from LonLat, Size, Pixel or Bounds.<br><br clear="all">Li XinGang<br>EMail: <a href="mailto:slinavlee@gmail.com">slinavlee@gmail.com</a><br>Blog:   <a href="http://avlee.cnblogs.com">avlee.cnblogs.com</a><br>

Site:    <a href="http://www.mapboost.org">www.mapboost.org</a><br>
<br><br><div class="gmail_quote">On Sat, Oct 16, 2010 at 8:37 PM, Eric Lemoine <span dir="ltr">&lt;<a href="mailto:eric.lemoine@camptocamp.com">eric.lemoine@camptocamp.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

Hi<br>
<br>
When defining the roadmap for OpenLayers 3 we mentioned drag/pan<br>
performance. Profiling with FireBug shows that a great deal of time is<br>
spent in the Class function, which is the inner function of<br>
OpenLayers.Class [*]. This is because (a) we create lots of objects<br>
(Bounds, LonLat) when panning, and (b) creating objects has a cost:<br>
<br>
&gt;&gt;&gt; A = OpenLayers.Class({initialize: function() {}});<br>
&gt;&gt;&gt; start = new Date; for(var i=0,l=100000; i&lt;l; i++) { var a = new A;} stop = new Date; console.log(stop.getTime()-start.getTime());<br>
500<br>
<br>
If type A is defined using plain JavaScript then creating objects<br>
isn&#39;t as costly:<br>
<br>
&gt;&gt;&gt; A = function() {};<br>
&gt;&gt;&gt; start = new Date; for(var i=0,l=100000; i&lt;l; i++) { var a = new A;} stop = new Date; console.log(stop.getTime()-start.getTime());<br>
&gt;&gt;&gt; 270<br>
<br>
So should we consider changing the way we create types in OpenLayers?<br>
(I&#39;d think so.)<br>
<br>
First, for types that do not extend others, I think we should use<br>
plain JavaScript:<br>
<br>
A = function() {}; // constructor<br>
A.prototype = {}; // prototype definition<br>
<br>
For inheritance I&#39;d suggest that we provide a simple function, whose<br>
usage would be something like this:<br>
<br>
// the super class<br>
P = function() {};<br>
// the sub-class<br>
C = function() {<br>
    P.call(this); // to call parent constructor<br>
};<br>
OpenLayers.inherit(C, P);<br>
<br>
With OpenLayers.inherit looking like this:<br>
<br>
OpenLayers.inherit = function(C, P) {<br>
    var F = function() {};<br>
    F.prototype = P.prototype;<br>
    C.prototype = new F;<br>
<br>
    var i, l, o;<br>
    for(i=2, l=arguments.length; i&lt;l; i++) {<br>
        o = arguments[i];<br>
        if(typeof o === &quot;function&quot;) {<br>
            o = o.prototype;<br>
        }<br>
        OpenLayers.Util.extend(C.prototype, o);<br>
    }<br>
<br>
    return C;<br>
};<br>
<br>
With something like this we&#39;d incur no performance penalty because of<br>
the way we create objects. Plus, this is simple, and reduces the size<br>
of the code a bit.<br>
<br>
Thoughts? Comments?<br>
<br>
<br>
[*] &lt;<a href="http://github.com/openlayers/openlayers/blob/master/lib/OpenLayers/BaseTypes/Class.js#L22" target="_blank">http://github.com/openlayers/openlayers/blob/master/lib/OpenLayers/BaseTypes/Class.js#L22</a>&gt;<br>


<br>
--<br>
Eric Lemoine<br>
<br>
Camptocamp France SAS<br>
Savoie Technolac, BP 352<br>
73377 Le Bourget du Lac, Cedex<br>
<br>
Tel : 00 33 4 79 44 44 96<br>
Mail : <a href="mailto:eric.lemoine@camptocamp.com">eric.lemoine@camptocamp.com</a><br>
<a href="http://www.camptocamp.com" target="_blank">http://www.camptocamp.com</a><br>
_______________________________________________<br>
Dev mailing list<br>
<a href="mailto:Dev@lists.osgeo.org">Dev@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/mailman/listinfo/openlayers-dev" target="_blank">http://lists.osgeo.org/mailman/listinfo/openlayers-dev</a><br>
</blockquote></div><br>