Implement a row of simple optimizations to increase performance of Item constructors.

This commit is contained in:
Jürg Lehni 2013-04-07 17:36:09 -07:00
parent 232ea221b4
commit 218732e320
8 changed files with 14 additions and 10 deletions

View file

@ -70,7 +70,7 @@ this.Base = Base.inject(/** @lends Base# */{
* @return {Boolean} {@true if the object is a plain object} * @return {Boolean} {@true if the object is a plain object}
*/ */
_set: function(props) { _set: function(props) {
if (Base.isPlainObject(props)) { if (props && Base.isPlainObject(props)) {
for (var key in props) for (var key in props)
if (props.hasOwnProperty(key) && key in this) if (props.hasOwnProperty(key) && key in this)
this[key] = props[key]; this[key] = props[key];

View file

@ -93,7 +93,7 @@ var Group = this.Group = Item.extend(/** @lends Group# */{
// Allow Group to have children and named children // Allow Group to have children and named children
this._children = []; this._children = [];
this._namedChildren = {}; this._namedChildren = {};
if (!this._set(arg)) if (arg && !this._set(arg))
this.addChildren(Array.isArray(arg) ? arg : arguments); this.addChildren(Array.isArray(arg) ? arg : arguments);
}, },

View file

@ -235,7 +235,8 @@ var Item = this.Item = Base.extend(Callback, {
* }); * });
*/ */
set: function(props) { set: function(props) {
this._set(props); if (props)
this._set(props);
return this; return this;
}, },

View file

@ -72,7 +72,7 @@ var PlacedSymbol = this.PlacedSymbol = PlacedItem.extend(/** @lends PlacedSymbol
this.base(arg1 !== undefined && Point.read(arguments, 1)); this.base(arg1 !== undefined && Point.read(arguments, 1));
// If we can handle setting properties through object literal, we're all // If we can handle setting properties through object literal, we're all
// set. Otherwise we need to set symbol. // set. Otherwise we need to set symbol.
if (!this._set(arg0)) if (arg0 && !this._set(arg0))
this.setSymbol(arg0 instanceof Symbol ? arg0 : new Symbol(arg0)); this.setSymbol(arg0 instanceof Symbol ? arg0 : new Symbol(arg0));
}, },

View file

@ -54,7 +54,7 @@ var CompoundPath = this.CompoundPath = PathItem.extend(/** @lends CompoundPath#
// CompoundPath has children and supports named children. // CompoundPath has children and supports named children.
this._children = []; this._children = [];
this._namedChildren = {}; this._namedChildren = {};
if (!this._set(arg)) if (arg && !this._set(arg))
this.addChildren(Array.isArray(arg) ? arg : arguments); this.addChildren(Array.isArray(arg) ? arg : arguments);
}, },

View file

@ -13,7 +13,11 @@
Path.inject({ statics: new function() { Path.inject({ statics: new function() {
function createPath(args) { function createPath(args) {
return new Path().set(Base.getNamed(args)); var path = new Path(),
named = Base.getNamed(args);
if (named)
path._set(named);
return path;
} }
function createRectangle(/* rect */) { function createRectangle(/* rect */) {

View file

@ -86,7 +86,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
? arguments ? arguments
: null; : null;
this.setSegments(segments || []); this.setSegments(segments || []);
if (!segments) if (arg && !segments)
this._set(arg); this._set(arg);
}, },

View file

@ -41,7 +41,7 @@ var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{
this._paragraphStyle = ParagraphStyle.create(this); this._paragraphStyle = ParagraphStyle.create(this);
// See if a point is passed, and if so, pass it on to base(). If not, it // See if a point is passed, and if so, pass it on to base(). If not, it
// might be a properties object literal for #setPropeties() at the end. // might be a properties object literal for #setPropeties() at the end.
var hasProperties = Base.isPlainObject(arg) var hasProperties = arg && Base.isPlainObject(arg)
&& arg.x === undefined && arg.y === undefined; && arg.x === undefined && arg.y === undefined;
this.base(hasProperties ? null : Point.read(arguments)); this.base(hasProperties ? null : Point.read(arguments));
// No need to call setStyle(), since base() handles this already. // No need to call setStyle(), since base() handles this already.
@ -49,9 +49,8 @@ var TextItem = this.TextItem = Item.extend(/** @lends TextItem# */{
this.setParagraphStyle(); this.setParagraphStyle();
this._content = ''; this._content = '';
this._lines = []; this._lines = [];
if (hasProperties) { if (hasProperties)
this._set(arg); this._set(arg);
}
}, },
/** /**