Skip to content

Commit

Permalink
⚡️ feat: opmitize performance
Browse files Browse the repository at this point in the history
  • Loading branch information
niketpathak committed May 19, 2024
1 parent 90b2ec8 commit 2faebcb
Showing 1 changed file with 21 additions and 28 deletions.
49 changes: 21 additions & 28 deletions src/trie/trie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,27 @@ export const Trie: TrieType<any> = (config = {}) => {
function add(data: string | string[] | Dictionary[], identifier = '', identity?: (item?: unknown) => void) {
if (!data) return;

let node = root;
let token;
data = (data.constructor === Array ? data : [data]) as [];

data.forEach((value: string | Dictionary) => {
// we tokenize the incoming data to make search possible by fragments
const dataTokens = tokenize(typeof value === 'string' ? value : (value[identifier] as string));
dataTokens
.filter((item) => item) // filter out falsy values
.forEach((prefix) => {
node = root;

for (let i = 0, l = prefix.length; i < l; i++) {
token = prefix[i];
node = (node[token] || (node[token] = {})) as Record<string, unknown>;
}

const uniqueId = typeof value === 'string' ? value : (identity && identity(value)) || JSON.stringify(value);

if (!node[SENTINEL]) {
node[SENTINEL] = {
[uniqueId]: value,
};
} else {
(node[SENTINEL] as Dictionary)[uniqueId] = value;
}
});
});
let node: Record<string, unknown>;
data = Array.isArray(data) ? data : [data];
const isStringArr = typeof data[0] === 'string';

for (const value of data) {
// we tokenize the incoming data to make search possible by fragments and also filter out falsy values
const dataTokens = tokenize(
isStringArr ? (value as string) : ((value as Dictionary)[identifier] as string)
).filter(Boolean);
for (const prefix of dataTokens) {
node = root;

for (const char of prefix) {
node = (node[char] ||= {}) as Record<string, unknown>;
}

const uniqueId = isStringArr ? value : (identity && identity(value)) || JSON.stringify(value);
const sentinelNode = (node[SENTINEL] ??= {});
(sentinelNode as Dictionary)[uniqueId as string] = value;
}
}
}

/**
Expand Down

0 comments on commit 2faebcb

Please sign in to comment.