Skip to content

Commit

Permalink
perf(dia.Cell): make prop()'s internal set() call more efficient (#2329)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumilingus authored Sep 12, 2023
1 parent e0a5aac commit 4f8c058
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/dia/Cell.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,15 @@ export const Cell = Backbone.Model.extend({
options.rewrite = false;
}

return this.set(merge({}, this.attributes, props), options);
// Create a new object containing only the changed attributes.
const changedAttributes = {};
for (const key in props) {
// Merging the values of changed attributes with the current ones.
const { changedValue } = merge({}, { changedValue: this.attributes[key] }, { changedValue: props[key] });
changedAttributes[key] = changedValue;
}

return this.set(changedAttributes, options);
},

// A convenient way to unset nested properties
Expand Down
23 changes: 23 additions & 0 deletions test/jointjs/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,29 @@ QUnit.module('cell', function(hooks) {
assert.equal(el.prop('name/second'), 'doe');
});

QUnit.test('path and value as object is set effectively', function(assert) {

// This test is here to make sure that the `set` method is called only once
// when setting multiple attributes at once and only the changed attributes
// are passed to the `set` method.

el.set('otherAttribute', 'otherValue');

const setSpy = sinon.spy(el, 'set');
let attributes;

el.prop({ age: 20, name: { first: 'john' }});
assert.ok(setSpy.calledOnce);
[attributes] = setSpy.lastCall.args;
assert.deepEqual(attributes, { age: 20, name: { first: 'john' }});

el.prop({ name: { second: 'doe' }});
assert.ok(setSpy.calledTwice);
[attributes] = setSpy.lastCall.args;
assert.deepEqual(attributes, { name: { first: 'john', second: 'doe' }});

setSpy.restore();
});

QUnit.test('path as top-level attribute name and value as object', function(assert) {

Expand Down

0 comments on commit 4f8c058

Please sign in to comment.