-
Notifications
You must be signed in to change notification settings - Fork 838
perf: optimize countWords function for large payloads #585
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
Changes from 2 commits
291175e
5f05d26
46192c9
4c86a7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --- | ||
| --- | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -174,18 +174,43 @@ export class LingoDotDevEngine { | |
| * @param payload - The payload to count words in | ||
| * @returns The total number of words | ||
| */ | ||
| // private countWordsInRecord(payload: any | Record<string, any> | Array<any>): number { | ||
| // if (Array.isArray(payload)) { | ||
| // return payload.reduce((acc, item) => acc + this.countWordsInRecord(item), 0); | ||
| // } else if (typeof payload === "object" && payload !== null) { | ||
| // return Object.values(payload).reduce((acc: number, item) => acc + this.countWordsInRecord(item), 0); | ||
| // } else if (typeof payload === "string") { | ||
| // return payload.trim().split(/\s+/).filter(Boolean).length; | ||
| // } else { | ||
| // return 0; | ||
| // } | ||
| // } | ||
|
|
||
| private countWordsInRecord(payload: any | Record<string, any> | Array<any>): number { | ||
| if (Array.isArray(payload)) { | ||
| return payload.reduce((acc, item) => acc + this.countWordsInRecord(item), 0); | ||
| } else if (typeof payload === "object" && payload !== null) { | ||
| return Object.values(payload).reduce((acc: number, item) => acc + this.countWordsInRecord(item), 0); | ||
| } else if (typeof payload === "string") { | ||
| return payload.trim().split(/\s+/).filter(Boolean).length; | ||
| } else { | ||
| return 0; | ||
| const stack: any[] = [payload]; | ||
| let totalWordCount = 0; | ||
|
|
||
| while (stack.length > 0) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is this an optimization? If I am reading this right, the code is executed the same number of times as before. This is a micro-optimization at best (if I'd like a second pair of eyes on this, @maxprilutskiy @7hacker please have a look.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mathio I can come up with a better optimization for this making it more readable.Should I make a new pr or add the changes in this branch?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I raised my concerns in the issue. Lets continue the conversation there.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| const current = stack.pop(); | ||
|
|
||
| if (current === null || current === undefined) { | ||
| continue; | ||
| } | ||
|
|
||
| if (Array.isArray(current)) { | ||
| stack.push(...current); | ||
| } else if (typeof current === 'object') { | ||
| stack.push(...Object.values(current)); | ||
| } else if (typeof current === 'string') { | ||
| totalWordCount += current.trim().split(/\s+/).filter(Boolean).length; | ||
| } | ||
| } | ||
|
|
||
| return totalWordCount; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Localize a typical JavaScript object | ||
| * @param obj - The object to be localized (strings will be extracted and translated) | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs a changeset if we want to release the change.