-
Notifications
You must be signed in to change notification settings - Fork 5
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
Conversation
helpers/legacyStore.js
Outdated
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) { |
There was a problem hiding this comment.
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/
There was a problem hiding this comment.
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.
helpers/legacyStore.js
Outdated
} | ||
|
||
var getUniqueId = function (items) { | ||
var id = nextUniqueId(); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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).
It's expected that passed data is not changed. I consider changing stored data by reference an invalid use.
Is this amortized? I thought it was called every POST?
…Sent from my iPhone
On May 14, 2017, at 8:02 PM, Chris Andrejewski ***@***.***> wrote:
@andrejewski commented on this pull request.
In helpers/legacyStore.js:
> 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) {
+ if (items[key].id === id) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ var getUniqueId = function (items) {
+ var id = nextUniqueId();
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).
—
You are receiving this because your review was requested.
Reply to this email directly, view it on GitHub, or mute the thread.
|
In practice, I've created fixtures with 1000s of items. It's common to use fixtures to simulate large amounts of data.
…Sent from my iPhone
On May 14, 2017, at 8:02 PM, Chris Andrejewski ***@***.***> wrote:
@andrejewski commented on this pull request.
In helpers/legacyStore.js:
> 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) {
+ if (items[key].id === id) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ var getUniqueId = function (items) {
+ var id = nextUniqueId();
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).
—
You are receiving this because your review was requested.
Reply to this email directly, view it on GitHub, or mute the thread.
|
There's a bunch of places like this in our projects where we assume that data should not be mutated such as an event object's type when it's dispatched or initial props passed to new DefineMap. We could seal or clone these objects, but it's generally not worth it because users have a similar expectation. Perhaps we need a way to make this explicit ... that certain objects passed to functions should not be mutated.
…Sent from my iPhone
On May 14, 2017, at 9:06 PM, Justin Meyer ***@***.***> wrote:
It's expected that passed data is not changed. I consider changing stored data by reference an invalid use.
Is this amortized? I thought it was called every POST?
Sent from my iPhone
> On May 14, 2017, at 8:02 PM, Chris Andrejewski ***@***.***> wrote:
>
> @andrejewski commented on this pull request.
>
> In helpers/legacyStore.js:
>
> > 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) {
> + if (items[key].id === id) {
> + return false;
> + }
> + }
> + return true;
> + }
> +
> + var getUniqueId = function (items) {
> + var id = nextUniqueId();
> 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).
>
> —
> You are receiving this because your review was requested.
> Reply to this email directly, view it on GitHub, or mute the thread.
>
|
837e98f
to
ffd0b74
Compare
No description provided.