Small performance improvements (part 1)

This commit is contained in:
impinball 2015-12-16 10:51:33 -05:00
parent 75a3f0785c
commit 69836f3811

View file

@ -539,7 +539,8 @@
if (!keysDiffer) { if (!keysDiffer) {
forKeys(inst.data, function (attrs, i) { forKeys(inst.data, function (attrs, i) {
var cachedCell = inst.cached[i] var cachedCell = inst.cached[i]
return keysDiffer = cachedCell && return keysDiffer =
cachedCell &&
cachedCell.attrs && cachedCell.attrs &&
cachedCell.attrs.key !== attrs.key cachedCell.attrs.key !== attrs.key
}) })
@ -601,8 +602,8 @@
case MOVE: case MOVE:
var changeElement = change.element var changeElement = change.element
if (inst.parent.childNodes[index] !== changeElement && // changeElement is never null
changeElement !== null) { if (inst.parent.childNodes[index] !== changeElement) {
inst.parent.insertBefore( inst.parent.insertBefore(
changeElement, changeElement,
inst.parent.childNodes[index] || null inst.parent.childNodes[index] || null
@ -764,7 +765,7 @@
if (index > -1) { if (index > -1) {
return cached[index] return cached[index]
} else if (typeof controller === "function") { } else if (isFunction(controller)) {
return new controller() return new controller()
} else { } else {
return {} return {}
@ -790,14 +791,19 @@
} }
} }
// shallow array compare, sorts // shallow array compare, assumes strings
function arraySortCompare(a, b) { function arraySortCompare(a, b) {
a.sort()
b.sort()
var len = a.length var len = a.length
if (len !== b.length) return false if (len !== b.length) return false
for (var i = 0; i < len; i++) {
if (a[i] !== b[i]) return false // A string-integer map is used to simplify the algorithm from
// two `O(n * log(n))` loops + an `O(n)` loop to just two O(n) loops
// with constant-time (or a super cheap `log(n)`) string key lookup.
var i = 0
var cache = Object.create(null)
while (i < len) cache[b[i]] = i++
while (i !== 0) {
if (cache[a[--i]] === undefined) return false
} }
return true return true
} }
@ -858,8 +864,7 @@
return $document.createElement(data.tag) return $document.createElement(data.tag)
} }
} else if (data.attrs.is) { } else if (data.attrs.is) {
return $document.createElementNS(inst.ns, data.tag, return $document.createElementNS(inst.ns, data.tag, data.attrs.is)
data.attrs.is)
} else { } else {
return $document.createElementNS(inst.ns, data.tag) return $document.createElementNS(inst.ns, data.tag)
} }
@ -893,7 +898,7 @@
function objectBuildChildren(inst, node) { function objectBuildChildren(inst, node) {
var children = inst.builder.data.children var children = inst.builder.data.children
if (children != null && children.length !== 0) { if (children != null && children.length) {
return objectMakeChild(inst, node, true) return objectMakeChild(inst, node, true)
} else { } else {
return children return children
@ -995,22 +1000,8 @@
} }
function nodeHasBody(node) { function nodeHasBody(node) {
return node !== "AREA" && return !/^(AREA|BASE|BR|COL|COMMAND|EMBED|HR|IMG|INPUT|KEYGEN|LINK|META|PARAM|SOURCE|TRACK|WBR)$/ // eslint-disable-line max-len
node !== "BASE" && .test(node)
node !== "BR" &&
node !== "COL" &&
node !== "COMMAND" &&
node !== "EMBED" &&
node !== "HR" &&
node !== "IMG" &&
node !== "INPUT" &&
node !== "KEYGEN" &&
node !== "LINK" &&
node !== "META" &&
node !== "PARAM" &&
node !== "SOURCE" &&
node !== "TRACK" &&
node !== "WBR"
} }
function builderHandleNonexistentNodes(inst) { function builderHandleNonexistentNodes(inst) {
@ -1112,12 +1103,7 @@
} }
function shouldSetAttrDirectly(attr) { function shouldSetAttrDirectly(attr) {
return attr !== "list" && return !/^(list|style|form|type|width|height)$/.test(attr)
attr !== "style" &&
attr !== "form" &&
attr !== "type" &&
attr !== "width" &&
attr !== "height"
} }
function trySetAttribute(attr, dataAttr, cachedAttr, node, namespace, tag) { function trySetAttribute(attr, dataAttr, cachedAttr, node, namespace, tag) {
@ -1831,7 +1817,7 @@
function parseQueryString(str) { function parseQueryString(str) {
if (str === "" || str == null) return {} if (str === "" || str == null) return {}
if (str.charAt(0) === "?") str = str.slice(1) if (str[0] === "?") str = str.slice(1)
var pairs = str.split("&") var pairs = str.split("&")
var params = {} var params = {}