Skip to content
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

perf(ivy): improve styling performance #33326

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'
]);
});
});
});