[OpenLayers-Dev] [OpenLayers 3] type instantiation performance

Andreas Hocevar ahocevar at opengeo.org
Sat Oct 16 14:57:22 EDT 2010


Hi,

my opinion is that we can make much more use of object literals internally. Which means e.g. to say

var a = {x: 23.4, y: 42.4};

instead of

var a = new OpenLayers.Pixel(23.4, 42.4);

We can do this wherever we just pass on values, without the need to call any memeber functions.

Regards,
Andreas.

On Oct 16, 2010, at 14:37 , Eric Lemoine wrote:

> Hi
> 
> When defining the roadmap for OpenLayers 3 we mentioned drag/pan
> performance. Profiling with FireBug shows that a great deal of time is
> spent in the Class function, which is the inner function of
> OpenLayers.Class [*]. This is because (a) we create lots of objects
> (Bounds, LonLat) when panning, and (b) creating objects has a cost:
> 
>>>> A = OpenLayers.Class({initialize: function() {}});
>>>> start = new Date; for(var i=0,l=100000; i<l; i++) { var a = new A;} stop = new Date; console.log(stop.getTime()-start.getTime());
> 500
> 
> If type A is defined using plain JavaScript then creating objects
> isn't as costly:
> 
>>>> A = function() {};
>>>> start = new Date; for(var i=0,l=100000; i<l; i++) { var a = new A;} stop = new Date; console.log(stop.getTime()-start.getTime());
>>>> 270
> 
> So should we consider changing the way we create types in OpenLayers?
> (I'd think so.)
> 
> First, for types that do not extend others, I think we should use
> plain JavaScript:
> 
> A = function() {}; // constructor
> A.prototype = {}; // prototype definition
> 
> For inheritance I'd suggest that we provide a simple function, whose
> usage would be something like this:
> 
> // the super class
> P = function() {};
> // the sub-class
> C = function() {
>    P.call(this); // to call parent constructor
> };
> OpenLayers.inherit(C, P);
> 
> With OpenLayers.inherit looking like this:
> 
> OpenLayers.inherit = function(C, P) {
>    var F = function() {};
>    F.prototype = P.prototype;
>    C.prototype = new F;
> 
>    var i, l, o;
>    for(i=2, l=arguments.length; i<l; i++) {
>        o = arguments[i];
>        if(typeof o === "function") {
>            o = o.prototype;
>        }
>        OpenLayers.Util.extend(C.prototype, o);
>    }
> 
>    return C;
> };
> 
> With something like this we'd incur no performance penalty because of
> the way we create objects. Plus, this is simple, and reduces the size
> of the code a bit.
> 
> Thoughts? Comments?
> 
> 
> [*] <http://github.com/openlayers/openlayers/blob/master/lib/OpenLayers/BaseTypes/Class.js#L22>
> 
> -- 
> Eric Lemoine
> 
> Camptocamp France SAS
> Savoie Technolac, BP 352
> 73377 Le Bourget du Lac, Cedex
> 
> Tel : 00 33 4 79 44 44 96
> Mail : eric.lemoine at camptocamp.com
> http://www.camptocamp.com
> _______________________________________________
> Dev mailing list
> Dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/openlayers-dev



-- 
Andreas Hocevar
OpenGeo - http://opengeo.org/
Expert service straight from the developers.



More information about the Dev mailing list