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

Prevent undefined elements #33

Merged
merged 3 commits into from
Jul 19, 2022
Merged

Prevent undefined elements #33

merged 3 commits into from
Jul 19, 2022

Conversation

mateonunez
Copy link
Collaborator

This PR fixes #29.

The solution is to add, before the return statement, a filter by Boolean, so undefined elements are filtered.

After doing several benchmarks between filter method, dynamic array, and the current solution, the result makes the filter method more performant.

Let me show you some data.

100K elements

Inserting 100K elements, search something, and limit by 1.

Current fixed allocation array

{ elapsed: '80μs', hits: [ undefined ], count: 0 } { timeSpent: '1072ms' }

Dynamic allocation array

{ elapsed: '82μs', hits: [], count: 0 } { timeSpent: '1117ms' }

Filtering results

{ elapsed: '75μs', hits: [], count: 0 } { timeSpent: '1080ms' }

1M elements

Inserting 1M elements, search something, and limit by 10.

{
  elapsed: '79μs',
  hits: [
    undefined, undefined,
    undefined, undefined,
    undefined, undefined,
    undefined, undefined,
    undefined, undefined
  ],
  count: 0
} { timeSpent: '11836ms' }

Dynamic allocation array

{ elapsed: '88μs', hits: [], count: 0 } { timeSpent: '11822ms' }

Filtering results

{ elapsed: '81μs', hits: [], count: 0 } { timeSpent: '11068ms' }

I also cleaned some variable names, (more readable in my opinion) by having them with the same name as the props returned in the function.

@mateonunez mateonunez requested a review from micheleriva July 19, 2022 13:07
@micheleriva
Copy link
Member

Awesome! Thank you for this!

@micheleriva micheleriva merged commit 4f8b24b into oramasearch:main Jul 19, 2022
@mateonunez mateonunez deleted the fix/undefined-elements branch July 19, 2022 14:29
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.

Search method returns undefined elements
2 participants