Skip to content

Commit

Permalink
fix(memory): Use for loop in _find() for better performance (#2844)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaddyWarbucks authored Nov 3, 2022
1 parent 50e7463 commit d6ee5f1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
41 changes: 32 additions & 9 deletions packages/memory/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,49 @@ export class MemoryAdapter<T = any, D = Partial<T>, P extends Params = Params> e
const { paginate } = this.getOptions(params)
const { query, filters } = this.getQuery(params)

let values = _.values(this.store).filter(this.options.matcher(query))
let values = _.values(this.store)
const total = values.length
const hasSkip = filters.$skip !== undefined
const hasSort = filters.$sort !== undefined
const hasLimit = filters.$limit !== undefined
const hasQuery = _.keys(query).length > 0

if (filters.$sort !== undefined) {
if (hasSort) {
values.sort(this.options.sorter(filters.$sort))
}

if (filters.$skip !== undefined) {
values = values.slice(filters.$skip)
}
if (hasQuery || hasLimit || hasSkip) {
let skipped = 0
const matcher = this.options.matcher(query)
const matched = []

for (let index = 0, length = values.length; index < length; index++) {
const value = values[index]

if (hasQuery && !matcher(value, index, values)) {
continue
}

if (hasSkip && filters.$skip > skipped) {
skipped++
continue
}

matched.push(_select(value, params))

if (hasLimit && filters.$limit === matched.length) {
break
}
}

if (filters.$limit !== undefined) {
values = values.slice(0, filters.$limit)
values = matched
}

const result: Paginated<T> = {
total,
total: hasQuery ? values.length : total,
limit: filters.$limit,
skip: filters.$skip || 0,
data: values.map((value) => _select(value, params))
data: filters.$limit === 0 ? [] : values
}

if (!paginate) {
Expand Down
2 changes: 1 addition & 1 deletion packages/memory/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ describe('Feathers Memory Service', () => {
)

await app.service('matcher').find({
query: { $sort: { something: 1 } }
query: { something: 1, $sort: { something: 1 } }
})

assert.ok(sorterCalled, 'sorter called')
Expand Down

0 comments on commit d6ee5f1

Please # to comment.