Skip to content

Commit

Permalink
perf(utils): increase max arguments & avoid shallow clone
Browse files Browse the repository at this point in the history
Co-authored-by: Alexandr Pestryakov <mertico@yandex.ru>
  • Loading branch information
H4ad and Mertico committed Sep 6, 2023
1 parent 9530055 commit b21577c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/orama/src/methods/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
InternalDocumentID,
} from '../components/internal-document-id-store.js'
import { createError } from '../errors.js'
import { getNanosecondsTime, getNested, sortTokenScorePredicate, safeAddNewItems, safeArrayPush } from '../utils.js';
import { getNanosecondsTime, getNested, sortTokenScorePredicate, safeArrayPush } from '../utils.js';

const defaultBM25Params: BM25Params = {
k: 1.2,
Expand Down
8 changes: 4 additions & 4 deletions packages/orama/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ export const isServer = typeof window === 'undefined'
* But i don't know if this value change from nodejs to nodejs
* So I will keep a safer value here.
*/
export const MAX_ARGUMENT_FOR_STACK = 10_000;
export const MAX_ARGUMENT_FOR_STACK = 65535;

/**
* This method is needed to used because of issues like: https://github.com/oramasearch/orama/issues/301
* that issue is caused because the array that is pushed is huge (>100k)
*
*
* @example
* ```ts
* safeArrayPush(myArray, [1, 2])
* ```
*/
export function safeArrayPush<T>(arr: T[], newArr: T[]): void {
if (newArr.length < MAX_ARGUMENT_FOR_STACK) {
arr.push(...newArr)
Array.prototype.push.apply(arr, newArr)
} else {
for (let i = 0; i < newArr.length; i += MAX_ARGUMENT_FOR_STACK) {
arr.push(...newArr.slice(i, i + MAX_ARGUMENT_FOR_STACK))
Array.prototype.push.apply(arr, newArr.slice(i, i + MAX_ARGUMENT_FOR_STACK))
}
}
}
Expand Down

0 comments on commit b21577c

Please sign in to comment.