Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve putSync performance of Memory store. #226

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 50 additions & 33 deletions Memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,50 +76,67 @@ define([
storage.version++;

var eventType = id in index ? 'update' : 'add',
event = { target: object },
previousIndex,
defaultDestination;
if (eventType === 'update') {
if (options.overwrite === false) {
throw new Error('Object already exists');
event = { target: object };

var prevIndex, destIndex, beforeIndex;
// evaluate options first
if ('beforeId' in options) {
if (!options.beforeId in index) {
console.error('options.beforeId was specified but no corresponding index was found');
} else {
data.splice(previousIndex = index[id], 1);
defaultDestination = previousIndex;
beforeIndex = index[options.beforeId];
}
}
if ('overwrite' in options) {
if (eventType === 'update' && options.overwrite === false) {
throw new Error('Object already exists');
}
} else {
defaultDestination = this.defaultNewToStart ? 0 : data.length;
}

var destination;
if ('beforeId' in options) {
var beforeId = options.beforeId;

if (beforeId === null) {
destination = data.length;
if (eventType === 'update') {
prevIndex = index[id]
if (isNaN(beforeIndex)) {
destIndex = prevIndex;
} else {
destination = index[beforeId];

// Account for the removed item
if (previousIndex < destination) {
--destination;
if (prevIndex === beforeIndex - 1) {
// if the moving element is already located one index before beforeIndex
destIndex = prevIndex;
} else if (prevIndex < beforeIndex) {
// if the moving element is located before beforeIndex
destIndex = beforeIndex - 1;
} else {
destIndex = beforeIndex;
}
}

if (destination !== undefined) {
event.beforeId = beforeId;
} else {
if (isNaN(beforeIndex)) {
if (this.defaultNewToStart) {
destIndex = 0;
} else {
destIndex = data.length;
}
} else {
console.error('options.beforeId was specified but no corresponding index was found');
destination = defaultDestination;
destIndex = beforeIndex;
}
}

if (prevIndex === destIndex) {
data[destIndex] = object;
} else {
destination = defaultDestination;
if (!isNaN(prevIndex)) {
data.splice(prevIndex, 1)
}
data.splice(destIndex, 0, object);
}
data.splice(destination, 0, object);

// the fullData has been changed, so the index needs updated
var i = isFinite(previousIndex) ? Math.min(previousIndex, destination) : destination;
for (var l = data.length; i < l; ++i) {
index[this.getIdentity(data[i])] = i;

// reindex if
// it is add operation OR
// the element was not updated at the same index
if (isNaN(prevIndex) || prevIndex !== destIndex) {
var i = isFinite(prevIndex) ? Math.min(prevIndex, destIndex) : destIndex;
for (var l = data.length; i < l; ++i) {
index[this.getIdentity(data[i])] = i;
}
}

this.emit(eventType, event);
Expand Down