Skip to content

Commit

Permalink
[PERF] tokenizer: avoid useless array map
Browse files Browse the repository at this point in the history
Avoid creating an array and mapping over an array (which allocates a new array)

Reduces memory allocation of `matchReference` by ~50% (from 12Mb to 6Mb on
RNG's large timesheet spreadsheet)

closes #4195

Task: 3920705
X-original-commit: 467d5a0
Signed-off-by: Pierre Rousseau (pro) <pro@odoo.com>
Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
  • Loading branch information
LucasLefevre committed May 13, 2024
1 parent 314d75d commit b2db1c0
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/formulas/range_tokenizer.ts
@@ -1,5 +1,4 @@
import {
concat,
isColHeader,
isColReference,
isRowHeader,
Expand Down Expand Up @@ -120,7 +119,7 @@ const machine: Machine = {
function matchReference(tokens: Token[]): Token | null {
let head = 0;
let transitions = machine[State.LeftRef];
const matchedTokens: Token[] = [];
let matchedTokens: string = "";
while (transitions !== undefined) {
const token = tokens[head++];
if (!token) {
Expand All @@ -132,15 +131,15 @@ function matchReference(tokens: Token[]): Token | null {
case undefined:
return null;
case State.Found:
matchedTokens.push(token);
matchedTokens += token.value;
tokens.splice(0, head);
return {
type: "REFERENCE",
value: concat(matchedTokens.map((token) => token.value)),
value: matchedTokens,
};
default:
transitions = machine[nextState];
matchedTokens.push(token);
matchedTokens += token.value;
break;
}
}
Expand Down

0 comments on commit b2db1c0

Please sign in to comment.