I am filtering a large javascript object (JSON file of 37000 lines) recursively in react. After i have filtered the object i send it down as props to a component that renders the object.
This is my current function and its working fine for all of the smaller files, but its pretty slow for the larger one's. Are there any improvements i could make to the function to make it perform faster?
export const filterBySubstringKey = (data: any, value: string) => { const iterate = (object: any, result: any) => { return Object.keys(object).reduce((acc, key) => { let tempResult = {}; if (key.toLowerCase().indexOf(value.toLowerCase()) !== -1) { result[key] = object[key]; return true; } if (object[key] !== null && typeof object[key] === 'object' && iterate(object[key], tempResult)) { result[key] = tempResult; return true; } return acc; }, false); }; let result = {}; iterate(data, result); return result; };
value.toLowerCase(), just do this once and store in a var.Object.keys, since you are potentially after the value too, you could maybe change toObject.entriesinstead ofobject[key]