Optimize handling of property object in Item constructor.

Only check for values if it is a plain object, and avoid inserting into DOM twice if parent is specified.
This commit is contained in:
Jürg Lehni 2014-04-13 18:12:19 +02:00
parent ee729622e0
commit 6af797bbad
2 changed files with 16 additions and 11 deletions

View file

@ -56,11 +56,13 @@ Base.inject(/** @lends Base# */{
* *
* @param {Object} props an object describing the properties to set * @param {Object} props an object describing the properties to set
* @param {Object} [exclude=undefined] a lookup table listing properties to * @param {Object} [exclude=undefined] a lookup table listing properties to
* exclude. * exclude
* @param {Boolean} [dontCheck=false] whether to perform a
* Base.isPlainObject() check on props or not
* @return {Boolean} {@true if the object is a plain object} * @return {Boolean} {@true if the object is a plain object}
*/ */
_set: function(props, exclude) { _set: function(props, exclude, dontCheck) {
if (props && Base.isPlainObject(props)) { if (props && (dontCheck || Base.isPlainObject(props))) {
// If props is a filtering object, we need to execute hasOwnProperty // If props is a filtering object, we need to execute hasOwnProperty
// on the original object (it's parent / prototype). See _filtered // on the original object (it's parent / prototype). See _filtered
// inheritance trick in the argument reading code. // inheritance trick in the argument reading code.

View file

@ -85,7 +85,8 @@ var Item = Base.extend(Callback, /** @lends Item# */{
_initialize: function(props, point) { _initialize: function(props, point) {
// Define this Item's unique id. But allow the creation of internally // Define this Item's unique id. But allow the creation of internally
// used paths with no ids. // used paths with no ids.
var internal = props && props.internal === true, var hasProps = props && Base.isPlainObject(props),
internal = hasProps && props.internal === true,
matrix = this._matrix = new Matrix(), matrix = this._matrix = new Matrix(),
project = paper.project; project = paper.project;
if (!internal) if (!internal)
@ -101,20 +102,22 @@ var Item = Base.extend(Callback, /** @lends Item# */{
// If _project is already set, the item was already moved into the DOM // If _project is already set, the item was already moved into the DOM
// hierarchy. Used by Layer, where it's added to project.layers instead // hierarchy. Used by Layer, where it's added to project.layers instead
if (!this._project) { if (!this._project) {
// Do not insert into DOM if it's an internal path or // Do not insert into DOM if it's an internal path, if props.insert
// props.insert is false. // is false, or if the props are setting a different parent anyway.
if (internal || props && props.insert === false) { if (internal || hasProps && props.insert === false) {
this._setProject(project); this._setProject(project);
} else { } else if (!hasProps || !props.parent) {
// Create a new layer if there is no active one. This will // Create a new layer if there is no active one. This will
// automatically make it the new activeLayer. // automatically make it the new activeLayer.
(project.activeLayer || new Layer()).addChild(this); (project.activeLayer || new Layer()).addChild(this);
} }
} }
// Filter out Item.NO_INSERT before _set(), for performance reasons. // Filter out Item.NO_INSERT before _set(), for performance reasons.
return props && props !== Item.NO_INSERT if (hasProps && props !== Item.NO_INSERT)
? this._set(props, { insert: true }) // Filter out insert prop. // Filter out insert property, and don't check for plain object
: true; // as we already do so through hasProps.
this._set(props, { insert: true }, true);
return hasProps;
}, },
_events: new function() { _events: new function() {