Skip to content

Commit a95c285

Browse files
committed
In profiling postcss I found that a significant amount of time was being
spent in [`unesc`](https://github.com/postcss/postcss-selector-parser/commits/master/src/util/unesc.js), this was due to the expensive regex checks that were being performed on the fly for every selector in the codebase which looked to be performing quite poorly inside of modern node and v8. ![image](https://user-images.githubusercontent.com/883126/114136698-fdd98a80-98bf-11eb-8068-ace4f6f2274d.png) ---- As an experiment and based on some prior experience with this class of slowdown I migrated the implementation to one that performs a scan through the string instead of running a regex replace. By testing this on my local application I instantly saw the work from this function go from > 900 ms to ~100ms. ![image](https://user-images.githubusercontent.com/883126/114136734-0c27a680-98c0-11eb-82ab-f0c9529fd32d.png) This implementation passes all of the existing test cases and aims to mirror the prior implementation's implementation details :) ----- Based on my application I am seeing the major wins come from purgecss dropping my total application build by multiple seconds! 🔥
1 parent 96a85e3 commit a95c285

File tree

3 files changed

+111
-16
lines changed

3 files changed

+111
-16
lines changed

src/__tests__/classes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,3 +264,4 @@ test('class selector with escaping (36)', '.not-pseudo\\:\\:focus', (t, tree) =>
264264
t.deepEqual(tree.nodes[0].nodes[0].type, 'class');
265265
t.deepEqual(tree.nodes[0].nodes[0].raws.value, 'not-pseudo\\:\\:focus');
266266
});
267+

src/__tests__/util/unesc.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {test} from '../util/helpers';
2+
3+
test('id selector', '#foo', (t, tree) => {
4+
t.deepEqual(tree.nodes[0].nodes[0].value, 'foo');
5+
});
6+
7+
test('escaped special char', '#w\\+', (t, tree) => {
8+
t.deepEqual(tree.nodes[0].nodes[0].value, 'w+');
9+
});
10+
11+
test('tailing escape', '#foo\\', (t, tree) => {
12+
t.deepEqual(tree.nodes[0].nodes[0].value, 'foo\\');
13+
});
14+
15+
test('double escape', '#wow\\\\k', (t, tree) => {
16+
t.deepEqual(tree.nodes[0].nodes[0].value, 'wow\\k');
17+
});
18+
19+
test('leading numeric', '.\\31 23', (t, tree) => {
20+
t.deepEqual(tree.nodes[0].nodes[0].value, '123');
21+
});
22+
23+
test('emoji', '.\\🐐', (t, tree) => {
24+
t.deepEqual(tree.nodes[0].nodes[0].value, '🐐');
25+
});
26+
27+
// https://www.w3.org/International/questions/qa-escapes#cssescapes
28+
test('hex escape', '.\\E9motion', (t, tree) => {
29+
t.deepEqual(tree.nodes[0].nodes[0].value, 'émotion');
30+
});
31+
32+
test('hex escape with space', '.\\E9 dition', (t, tree) => {
33+
t.deepEqual(tree.nodes[0].nodes[0].value, 'édition');
34+
});
35+
36+
test('hex escape with hex number', '.\\0000E9dition', (t, tree) => {
37+
t.deepEqual(tree.nodes[0].nodes[0].value, 'édition');
38+
});
39+
40+
test('class selector with escaping', '.\\1D306', (t, tree) => {
41+
t.deepEqual(tree.nodes[0].nodes[0].value, '𝌆');
42+
});
43+
44+
test('class selector with escaping with more chars', '.\\1D306k', (t, tree) => {
45+
t.deepEqual(tree.nodes[0].nodes[0].value, '𝌆k');
46+
});

src/util/unesc.js

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,67 @@
1-
const whitespace = '[\\x20\\t\\r\\n\\f]';
2-
const unescapeRegExp = new RegExp('\\\\([\\da-f]{1,6}' + whitespace + '?|(' + whitespace + ')|.)', 'ig');
1+
// Many thanks for this post which made this migration much easier.
2+
// https://mathiasbynens.be/notes/css-escapes
3+
4+
/**
5+
*
6+
* @param {string} str
7+
* @returns {[string, number]|undefined}
8+
*/
9+
function gobbleHex (str) {
10+
const lower = str.toLowerCase();
11+
let hex = '';
12+
let spaceTerminated = false;
13+
for (let i = 0; i < 6 && lower[i] !== undefined; i++) {
14+
const code = lower.charCodeAt(i);
15+
// check to see if we are dealing with a valid hex char [a-f|0-9]
16+
const valid = (code >= 97 && code <= 102) || (code >= 48 && code <= 57);
17+
// https://drafts.csswg.org/css-syntax/#consume-escaped-code-point
18+
spaceTerminated = code === 32;
19+
if (!valid) {
20+
break;
21+
}
22+
hex += lower[i];
23+
}
24+
25+
if (hex.length === 0) {
26+
return undefined;
27+
}
28+
29+
return [
30+
String.fromCodePoint(parseInt(hex, 16)),
31+
hex.length + (spaceTerminated ? 1 : 0),
32+
];
33+
}
334

435
export default function unesc (str) {
5-
return str.replace(unescapeRegExp, (_, escaped, escapedWhitespace) => {
6-
const high = '0x' + escaped - 0x10000;
7-
8-
// NaN means non-codepoint
9-
// Workaround erroneous numeric interpretation of +"0x"
10-
// eslint-disable-next-line no-self-compare
11-
return high !== high || escapedWhitespace
12-
? escaped
13-
: high < 0
14-
? // BMP codepoint
15-
String.fromCharCode(high + 0x10000)
16-
: // Supplemental Plane codepoint (surrogate pair)
17-
String.fromCharCode((high >> 10) | 0xd800, (high & 0x3ff) | 0xdc00);
18-
});
36+
let ret = "";
37+
38+
for (let i = 0; i < str.length; i++) {
39+
if ((str[i] === "\\")) {
40+
const gobbled = gobbleHex(str.slice(i+1, i+7));
41+
if (gobbled !== undefined) {
42+
ret += gobbled[0];
43+
i += gobbled[1];
44+
continue;
45+
}
46+
47+
// Retain a pair of \\ if double escaped `\\\\`
48+
// https://github.com/postcss/postcss-selector-parser/commit/268c9a7656fb53f543dc620aa5b73a30ec3ff20e
49+
if (str[i +1] === "\\") {
50+
ret += "\\";
51+
i++;
52+
continue;
53+
}
54+
55+
// if // is at the end of the string retain it
56+
// https://github.com/postcss/postcss-selector-parser/commit/01a6b346e3612ce1ab20219acc26abdc259ccefb
57+
if (str.length === i + 1) {
58+
ret += str[i];
59+
}
60+
continue;
61+
}
62+
63+
ret += str[i];
64+
}
65+
66+
return ret;
1967
}

0 commit comments

Comments
 (0)