Skip to content

Commit

Permalink
perf(utilities): optimize case-sensitive tag replace with hash map
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
remarkablemark committed Jun 6, 2019
1 parent 0862f39 commit 6aa06ee
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions lib/utilities.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
'use strict';

var CASE_SENSITIVE_TAG_NAMES = require('./constants').CASE_SENSITIVE_TAG_NAMES
var CASE_SENSITIVE_TAG_NAMES_LOWERCASE = CASE_SENSITIVE_TAG_NAMES.map(function(tag) {
return tag.toLowerCase();
});
var CASE_SENSITIVE_TAG_NAMES = require('./constants').CASE_SENSITIVE_TAG_NAMES;

var caseSensitiveTagNamesMap = {};
var tagName;
for (var i = 0, len = CASE_SENSITIVE_TAG_NAMES.length; i < len; i++) {
tagName = CASE_SENSITIVE_TAG_NAMES[i];
caseSensitiveTagNamesMap[tagName.toLowerCase()] = tagName;
}

/**
* Gets case-sensitive tag name.
*
* @param {String} tagName - The lowercase tag name.
* @return {String|undefined}
*/
function getCaseSensitiveTagName(tagName) {
return caseSensitiveTagNamesMap[tagName];
}

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

/**
* Correct the case of tag names. For case-sensitive SVG tags, return the proper
* tag name; for all other tags, return the lowercased name.
* Corrects the tag name if it is case-sensitive (SVG).
* Otherwise, returns the lowercase tag name (HTML).
*
* @param {String} tagName - The tag name to correct.
* @return {String} - The corrected tag name.
* @param {String} tagName - The lowercase tag name.
* @return {String} - The formatted tag name.
*/
function fixTagCase(tagName) {
var correctedName = tagName.toLowerCase();
var caseSensitiveTagIndex = CASE_SENSITIVE_TAG_NAMES_LOWERCASE.indexOf(correctedName);
// fix case if element is case-sensitive SVG
if (caseSensitiveTagIndex !== -1) {
correctedName = CASE_SENSITIVE_TAG_NAMES[caseSensitiveTagIndex];
function formatTagName(tagName) {
tagName = tagName.toLowerCase();
var caseSensitiveTagName = getCaseSensitiveTagName(tagName);
if (caseSensitiveTagName) {
return caseSensitiveTagName;
}
return correctedName;
return tagName;
}

/**
Expand Down Expand Up @@ -75,8 +88,8 @@ function formatDOM(nodes, parentObj, directive) {

// set the node name if it's not "#text" or "#comment"
// e.g., "div"
if (node.nodeName.indexOf('#') !== 0) {
nodeObj.name = fixTagCase(node.nodeName);
if (node.nodeName[0] !== '#') {
nodeObj.name = formatTagName(node.nodeName);
// also, nodes of type "tag" have "attribs"
nodeObj.attribs = {}; // default
if (node.attributes && node.attributes.length) {
Expand Down

0 comments on commit 6aa06ee

Please sign in to comment.