rocicorp / fractional-indexing

Fractional Indexing in JavaScript
https://npmjs.com/fractional-indexing
Creative Commons Zero v1.0 Universal
316 stars 23 forks source link

When you don't have an upper bound yet, what should you put for the second argument? #11

Closed floer32 closed 2 years ago

floer32 commented 2 years ago

Thanks for this awesome library. I have a usage question that's a little more specific than the README, and I wasn't sure of the answer from reviewing the test suite.

When inserting 10 new items, what's the best way to initialize their fractional index values? Would I iterate through them myself, first calling generateKeyBetween(null, null), and then passing first one to second one, and so on? Or if I know the initial size of my list, is this better:

generateNKeysBetween(null, null, 10)

Then, a related question. If there are 100 items in a db, with fractional index fields already populated, and I insert 10 more, should I grab the first/last of the existing values,[^1] and use that as an argument to generateKeyBetween/generateNKeysBetween? (Like an "offset")

[^1]: First or last would depend on whether the new items are intended to be prepended or appended, but I figure I can leave that unspecified for this question.

aboodman commented 2 years ago

Or if I know the initial size of my list, is this better: generateNKeysBetween(null, null, 10)

It's better to call generateNKeysBetween when you can because the indexes get spaced out evenly between the two end points. Imagine the bounds of the fractional indexes are 0 and 1. It's the difference between generating {0.2, 0.4, 0.6, 0.8} vs {0.5, 0.75, 0.875, 0.9375}.

If there are 100 items in a db, with fractional index fields already populated, and I insert 10 more

If you are ever inserting n keys at some known position, it's better to use generateNKeysBetween.

aboodman commented 2 years ago

Clarifying: The reason it's better to use generateNKeysBetween when possible is that it leads to shorter keys.

arv commented 2 years ago

I enabled "Discussions" for the next time a question comes up.