Skip to content

Commit 6aa06ee

Browse files
perf(utilities): optimize case-sensitive tag replace with hash map
The lookup cost of a hash map is O(1) instead of `indexOf` which is O(N). Generate a hash map of the case-sensitive tag names, rename `fixTagCase` to `formatTagName`, and use the hash map.
1 parent 0862f39 commit 6aa06ee

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

lib/utilities.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
'use strict';
22

3-
var CASE_SENSITIVE_TAG_NAMES = require('./constants').CASE_SENSITIVE_TAG_NAMES
4-
var CASE_SENSITIVE_TAG_NAMES_LOWERCASE = CASE_SENSITIVE_TAG_NAMES.map(function(tag) {
5-
return tag.toLowerCase();
6-
});
3+
var CASE_SENSITIVE_TAG_NAMES = require('./constants').CASE_SENSITIVE_TAG_NAMES;
4+
5+
var caseSensitiveTagNamesMap = {};
6+
var tagName;
7+
for (var i = 0, len = CASE_SENSITIVE_TAG_NAMES.length; i < len; i++) {
8+
tagName = CASE_SENSITIVE_TAG_NAMES[i];
9+
caseSensitiveTagNamesMap[tagName.toLowerCase()] = tagName;
10+
}
11+
12+
/**
13+
* Gets case-sensitive tag name.
14+
*
15+
* @param {String} tagName - The lowercase tag name.
16+
* @return {String|undefined}
17+
*/
18+
function getCaseSensitiveTagName(tagName) {
19+
return caseSensitiveTagNamesMap[tagName];
20+
}
721

822
/**
923
* Format DOM attributes to an associative array.
@@ -25,20 +39,19 @@ function formatAttributes(attributes) {
2539
}
2640

2741
/**
28-
* Correct the case of tag names. For case-sensitive SVG tags, return the proper
29-
* tag name; for all other tags, return the lowercased name.
42+
* Corrects the tag name if it is case-sensitive (SVG).
43+
* Otherwise, returns the lowercase tag name (HTML).
3044
*
31-
* @param {String} tagName - The tag name to correct.
32-
* @return {String} - The corrected tag name.
45+
* @param {String} tagName - The lowercase tag name.
46+
* @return {String} - The formatted tag name.
3347
*/
34-
function fixTagCase(tagName) {
35-
var correctedName = tagName.toLowerCase();
36-
var caseSensitiveTagIndex = CASE_SENSITIVE_TAG_NAMES_LOWERCASE.indexOf(correctedName);
37-
// fix case if element is case-sensitive SVG
38-
if (caseSensitiveTagIndex !== -1) {
39-
correctedName = CASE_SENSITIVE_TAG_NAMES[caseSensitiveTagIndex];
48+
function formatTagName(tagName) {
49+
tagName = tagName.toLowerCase();
50+
var caseSensitiveTagName = getCaseSensitiveTagName(tagName);
51+
if (caseSensitiveTagName) {
52+
return caseSensitiveTagName;
4053
}
41-
return correctedName;
54+
return tagName;
4255
}
4356

4457
/**
@@ -75,8 +88,8 @@ function formatDOM(nodes, parentObj, directive) {
7588

7689
// set the node name if it's not "#text" or "#comment"
7790
// e.g., "div"
78-
if (node.nodeName.indexOf('#') !== 0) {
79-
nodeObj.name = fixTagCase(node.nodeName);
91+
if (node.nodeName[0] !== '#') {
92+
nodeObj.name = formatTagName(node.nodeName);
8093
// also, nodes of type "tag" have "attribs"
8194
nodeObj.attribs = {}; // default
8295
if (node.attributes && node.attributes.length) {

0 commit comments

Comments
 (0)