[OpenLayers-Dev] [OpenLayers 3] type instantiation performance
Eric Lemoine
eric.lemoine at camptocamp.com
Sat Oct 16 08:37:45 EDT 2010
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
More information about the Dev
mailing list