perf(ivy): improve styling performance (#33326)
change the existing implementation from using ``` string.split(/\s+/); ``` to a char scan which performers the same thing. The reason why `split(/\s+/)` is slow is that: - `/\s+/` allocates new `RegExp` every time this code executes. - `RegExp` scans are a lot more expensive because they are more powerful. PR Close #33326
This commit is contained in:

committed by
Andrew Kushnir

parent
c79d6ec502
commit
d40ee6a259
@ -414,10 +414,8 @@ export function normalizeIntoStylingMap(
|
||||
let map: {[key: string]: any}|undefined|null;
|
||||
let allValuesTrue = false;
|
||||
if (typeof newValues === 'string') { // [class] bindings allow string values
|
||||
if (newValues.length) {
|
||||
props = newValues.split(/\s+/);
|
||||
allValuesTrue = true;
|
||||
}
|
||||
props = splitOnWhitespace(newValues);
|
||||
allValuesTrue = props !== null;
|
||||
} else {
|
||||
props = newValues ? Object.keys(newValues) : null;
|
||||
map = newValues;
|
||||
@ -435,6 +433,32 @@ export function normalizeIntoStylingMap(
|
||||
return stylingMapArr;
|
||||
}
|
||||
|
||||
export function splitOnWhitespace(text: string): string[]|null {
|
||||
let array: string[]|null = null;
|
||||
let length = text.length;
|
||||
let start = 0;
|
||||
let foundChar = false;
|
||||
for (let i = 0; i < length; i++) {
|
||||
const char = text.charCodeAt(i);
|
||||
if (char <= 32 /*' '*/) {
|
||||
if (foundChar) {
|
||||
if (array === null) array = [];
|
||||
array.push(text.substring(start, i));
|
||||
foundChar = false;
|
||||
}
|
||||
start = i + 1;
|
||||
} else {
|
||||
foundChar = true;
|
||||
}
|
||||
}
|
||||
if (foundChar) {
|
||||
if (array === null) array = [];
|
||||
array.push(text.substring(start, length));
|
||||
foundChar = false;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
// TODO (matsko|AndrewKushnir): refactor this once we figure out how to generate separate
|
||||
// `input('class') + classMap()` instructions.
|
||||
export function selectClassBasedInputName(inputs: PropertyAliases): string {
|
||||
|
Reference in New Issue
Block a user