Skip to content

Commit 5644203

Browse files
authored
avoid many property lookups with some local caches (#315)
1 parent 4ff1a04 commit 5644203

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/index.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,13 @@ function analyze(css) {
281281
break
282282
}
283283

284-
if (endsWith('\\9', node.unit)) {
285-
units.push(node.unit.substring(0, node.unit.length - 2), this.declaration.property)
284+
/** @type {string} */
285+
let unit = node.unit
286+
287+
if (endsWith('\\9', unit)) {
288+
units.push(unit.substring(0, unit.length - 2), this.declaration.property)
286289
} else {
287-
units.push(node.unit, this.declaration.property)
290+
units.push(unit, this.declaration.property)
288291
}
289292

290293
return this.skip
@@ -400,6 +403,8 @@ function analyze(css) {
400403
}
401404

402405
walk(node, function (valueNode) {
406+
let nodeName = valueNode.name
407+
403408
switch (valueNode.type) {
404409
case 'Hash': {
405410
let hexLength = valueNode.value.length
@@ -412,15 +417,14 @@ function analyze(css) {
412417
return this.skip
413418
}
414419
case 'Identifier': {
415-
let { name } = valueNode
416420
// Bail out if it can't be a color name
417421
// 20 === 'lightgoldenrodyellow'.length
418422
// 3 === 'red'.length
419-
if (name.length > 20 || name.length < 3) {
423+
if (nodeName.length > 20 || nodeName.length < 3) {
420424
return this.skip
421425
}
422426
let stringified = stringifyNode(valueNode)
423-
let lowerCased = name.toLowerCase()
427+
let lowerCased = nodeName.toLowerCase()
424428

425429
if (namedColors.has(lowerCased)) {
426430
colors.push(stringified, property)
@@ -436,10 +440,10 @@ function analyze(css) {
436440
}
437441
case 'Function': {
438442
// Don't walk var() multiple times
439-
if (strEquals('var', valueNode.name)) {
443+
if (strEquals('var', nodeName)) {
440444
return this.skip
441445
}
442-
let fnName = valueNode.name.toLowerCase()
446+
let fnName = nodeName.toLowerCase()
443447
let stringified = stringifyNode(valueNode)
444448
if (colorFunctions.has(fnName)) {
445449
colors.push(stringified, property)

src/string-utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ export function endsWith(base, test) {
5858
/**
5959
* Case-insensitive testing whether a string starts with a given substring
6060
* @param {string} base
61-
* @param {test} test
61+
* @param {string} test
6262
* @returns {boolean} true if `test` starts with `base`, false otherwise
6363
*/
6464
export function startsWith(base, test) {
6565
let len = base.length
6666
if (test.length < len) return false
6767

68-
for (let i = 0; i < base.length; i++) {
68+
for (let i = 0; i < len; i++) {
6969
if (compareChar(base.charCodeAt(i), test.charCodeAt(i)) === false) {
7070
return false
7171
}

0 commit comments

Comments
 (0)