From 6af797bbad04c546d0a959ba929d97b1d25fb06c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sun, 13 Apr 2014 18:12:19 +0200 Subject: [PATCH] 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. --- src/core/Base.js | 8 +++++--- src/item/Item.js | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/core/Base.js b/src/core/Base.js index fd1d7445..8494ac75 100644 --- a/src/core/Base.js +++ b/src/core/Base.js @@ -56,11 +56,13 @@ Base.inject(/** @lends Base# */{ * * @param {Object} props an object describing the properties to set * @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} */ - _set: function(props, exclude) { - if (props && Base.isPlainObject(props)) { + _set: function(props, exclude, dontCheck) { + if (props && (dontCheck || Base.isPlainObject(props))) { // If props is a filtering object, we need to execute hasOwnProperty // on the original object (it's parent / prototype). See _filtered // inheritance trick in the argument reading code. diff --git a/src/item/Item.js b/src/item/Item.js index fd202939..9611eef1 100644 --- a/src/item/Item.js +++ b/src/item/Item.js @@ -85,7 +85,8 @@ var Item = Base.extend(Callback, /** @lends Item# */{ _initialize: function(props, point) { // Define this Item's unique id. But allow the creation of internally // 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(), project = paper.project; 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 // hierarchy. Used by Layer, where it's added to project.layers instead if (!this._project) { - // Do not insert into DOM if it's an internal path or - // props.insert is false. - if (internal || props && props.insert === false) { + // Do not insert into DOM if it's an internal path, if props.insert + // is false, or if the props are setting a different parent anyway. + if (internal || hasProps && props.insert === false) { this._setProject(project); - } else { + } else if (!hasProps || !props.parent) { // Create a new layer if there is no active one. This will // automatically make it the new activeLayer. (project.activeLayer || new Layer()).addChild(this); } } // Filter out Item.NO_INSERT before _set(), for performance reasons. - return props && props !== Item.NO_INSERT - ? this._set(props, { insert: true }) // Filter out insert prop. - : true; + if (hasProps && props !== Item.NO_INSERT) + // Filter out insert property, and don't check for plain object + // as we already do so through hasProps. + this._set(props, { insert: true }, true); + return hasProps; }, _events: new function() {