Level / abstract-level

Abstract class for a lexicographically sorted key-value database.
MIT License
128 stars 8 forks source link

perf: allow for flat iterator response #23

Closed ronag closed 2 years ago

ronag commented 2 years ago

Requesting some feedback on whether or not this is a good idea?

vweevers commented 2 years ago

It's too many code paths. I'd prefer an approach where an implementation opts-in to support of this option, and must then return the data in the appropriate shape. Rather than abstract-level normalizing it (with an unexpected performance penalty).

Along the lines of:

this[kFlat] = db.supports.flat && options.flat === true
if (this[kFlat]) {
  for (let i = 0; i < items.length; i += 2) {
    const key = items[i]
    const value = items[i + 1]

    // ..
  }
} else {
  for (const item of items) {
    const key = item[0]
    const value = item[1]

    // ..
  }
}
ronag commented 2 years ago

Got it. I'll make a new PR.