Skip to content

Commit

Permalink
perf(ivy): improve styling performance (#33326)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mhevery authored and AndrewKushnir committed Oct 23, 2019
1 parent c79d6ec commit d40ee6a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
32 changes: 28 additions & 4 deletions packages/core/src/render3/util/styling_utils.ts
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions packages/core/test/render3/util/styling_utils_spec.ts
@@ -0,0 +1,26 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {splitOnWhitespace} from '@angular/core/src/render3/util/styling_utils';

describe('styling_utils', () => {
describe('splitOnWhitespace', () => {
it('should treat empty strings as null', () => {
expect(splitOnWhitespace('')).toEqual(null);
expect(splitOnWhitespace(' ')).toEqual(null);
expect(splitOnWhitespace(' \n\r\t ')).toEqual(null);
});

it('should split strings into parts', () => {
expect(splitOnWhitespace('a\nb\rc')).toEqual(['a', 'b', 'c']);
expect(splitOnWhitespace('\ta-long\nb-long\rc-long ')).toEqual([
'a-long', 'b-long', 'c-long'
]);
});
});
});

0 comments on commit d40ee6a

Please sign in to comment.