Performance: Built-in string functions
Using built-in string functions in Sass affects the CSS generation speed during build time, not the browser rendering performance.
.selector { content: str-slice("example", 2); }
@function slow-string($input) { @return unquote(str-slice($input, 2)); } .selector { content: slow-string("example"); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using custom string functions in Sass | 0 | 0 | 0 | [!] OK (build slower) |
| Using built-in Sass string functions | 0 | 0 | 0 | [OK] Good |
length("hello")length() functionlength() function in Sass returns how many characters are in a string.uppercase().uppercase("text"), which matches uppercase("text").$str: "Sass"; $result: str-insert($str, "3", 3);What is the value of
$result?str-insert() function$text: "Hello"; $new-text: str-slice($text, 2, 1);
str-slice() parameters@function first-three-uppercase($str) { @return str-slice(uppercase($str), 1, 3); } uppercases whole string, then slices first 3 characters -- correct.@function first-three-uppercase($str) { @return uppercase(str-slice($str, 1)); } str-slice from 1 to end (no end index), uppercases entire string -- incorrect, too many chars.@function first-three-uppercase($str) { @return str-insert(uppercase($str), 3, 1); } uses str-insert incorrectly and passes number 3 as insert string -- error.@function first-three-uppercase($str) { @return uppercase(str-insert($str, 3, 1)); } similar str-insert misuse -- incorrect.Pass the test and this pattern moves unknown