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

Make IDs unique based on existing items; fixes #102 #110

Merged
merged 2 commits into from
May 24, 2017

Conversation

andrejewski
Copy link
Contributor

No description provided.

var i = 0;
return function () {
return i++;
}
})();

// Check to see if an existing item has the ID we want to assign
var isIdUnique = function (items, id) {
for (var key in items) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is items list-like? If yes, better to use .forEach or can-util/js/each/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll use can-util's each, I do not think we can make the assumption it is list-like based on the current code and each will handle the object case.

}

var getUniqueId = function (items) {
var id = nextUniqueId();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might be able to calculate the right number only once and cache it. I think this is n^2.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is O(n^2). We can make it O(n) if we make a point-of-call cache. Since we don't copy the individual items if they are passed directly any item's ID can be changed throughout a test so non-point-of-call caching is ineffective. If you want to change nextUniqueId()'s i=0 to say i=100 to not bump into someone's items, that's fine as well. In test scenarios, the uniqueness guarantee (correctness) is more important than the minor amortized cost (performance).

@justinbmeyer
Copy link
Contributor

justinbmeyer commented May 15, 2017 via email

@justinbmeyer
Copy link
Contributor

justinbmeyer commented May 15, 2017 via email

@justinbmeyer
Copy link
Contributor

justinbmeyer commented May 15, 2017 via email

@andrejewski andrejewski merged commit 12d8d39 into master May 24, 2017
@andrejewski andrejewski deleted the 102-id-unique branch May 24, 2017 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants