Few more small performance tweaks

This commit is contained in:
impinball 2015-12-15 16:56:04 -05:00
parent bfc9dd45d4
commit 0e00621840
3 changed files with 39 additions and 55 deletions

View file

@ -539,7 +539,8 @@
if (!keysDiffer) {
forKeys(inst.data, function (attrs, i) {
var cachedCell = inst.cached[i]
return keysDiffer = cachedCell &&
return keysDiffer =
cachedCell &&
cachedCell.attrs &&
cachedCell.attrs.key !== attrs.key
})
@ -601,8 +602,7 @@
case MOVE:
var changeElement = change.element
if (inst.parent.childNodes[index] !== changeElement &&
changeElement !== null) {
if (inst.parent.childNodes[index] !== changeElement) {
inst.parent.insertBefore(
changeElement,
inst.parent.childNodes[index] || null
@ -764,7 +764,7 @@
if (index > -1) {
return cached[index]
} else if (typeof controller === "function") {
} else if (isFunction(controller)) {
return new controller()
} else {
return {}
@ -790,14 +790,19 @@
}
}
// shallow array compare, sorts
// Shallow array compare, assumes strings
function arraySortCompare(a, b) {
a.sort()
b.sort()
var len = a.length
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
}
@ -858,8 +863,7 @@
return $document.createElement(data.tag)
}
} else if (data.attrs.is) {
return $document.createElementNS(inst.ns, data.tag,
data.attrs.is)
return $document.createElementNS(inst.ns, data.tag, data.attrs.is)
} else {
return $document.createElementNS(inst.ns, data.tag)
}
@ -893,7 +897,7 @@
function objectBuildChildren(inst, node) {
var children = inst.builder.data.children
if (children != null && children.length !== 0) {
if (children != null && children.length) {
return objectMakeChild(inst, node, true)
} else {
return children
@ -995,22 +999,8 @@
}
function nodeHasBody(node) {
return node !== "AREA" &&
node !== "BASE" &&
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"
return !/^(AREA|BASE|BR|COL|COMMAND|EMBED|HR|IMG|INPUT|KEYGEN|LINK|META|PARAM|SOURCE|TRACK|WBR)$/ // eslint-disable-line max-len
.test(node)
}
function builderHandleNonexistentNodes(inst) {
@ -1053,10 +1043,9 @@
inst.editable.innerHTML = inst.data
} else {
// was a trusted string
if (nodes[0].nodeType === 1 ||
nodes.length > 1 ||
(nodes[0].nodeValue.trim && !nodes[0].nodeValue.trim())
) {
if (nodes[0].nodeType === 1 || nodes.length > 1 ||
isString(nodes[0].nodeValue) &&
/\s*/.test(nodes[0].nodeValue)) {
clear(inst.cached.nodes, inst.cached)
nodes = [$document.createTextNode(inst.data)]
}
@ -1112,12 +1101,7 @@
}
function shouldSetAttrDirectly(attr) {
return attr !== "list" &&
attr !== "style" &&
attr !== "form" &&
attr !== "type" &&
attr !== "width" &&
attr !== "height"
return !/^(list|style|form|type|width|height)$/.test(attr)
}
function trySetAttribute(attr, dataAttr, cachedAttr, node, namespace, tag) {
@ -1159,7 +1143,7 @@
// used as a string, but it's an object in js
//
// #348
// don't set the value if not needed otherwise cursor placement
// don't set the value if not needed, otherwise cursor placement
// breaks in Chrome
if (tag !== "input" || node[attr] !== dataAttr) {
node[attr] = dataAttr
@ -1175,7 +1159,7 @@
} catch (e) {
// swallow IE's invalid argument errors to mimic HTML's
// fallback-to-doing-nothing-on-invalid-attributes behavior
if (e.message.indexOf("Invalid argument") < 0) throw e
if (!/\bInvalid argument\b/.test(e.message)) throw e
}
}
@ -1187,7 +1171,7 @@
trySetSingle(attr, dataAttr, cachedAttr, node, namespace, tag)
} else if (attr === "value" && tag === "input" &&
// #348: dataAttr may not be a string, so use loose
// comparison
// comparison (i.e. identity not required).
node.value != dataAttr) { // eslint-disable-line eqeqeq
node.value = dataAttr
}
@ -1362,7 +1346,7 @@
function getCellCacheKey(element) {
var index = nodeCache.indexOf(element)
return index < 0 ? nodeCache.push(element) - 1 : index
return index >= 0 ? index : nodeCache.push(element) - 1
}
m.trust = function (value) {
@ -1468,7 +1452,7 @@
var lastRedrawCallTime = 0
function actuallyPerformRedraw() {
if (lastRedrawId > 0) $cancelAnimationFrame(lastRedrawId)
if (lastRedrawId !== 0) $cancelAnimationFrame(lastRedrawId)
lastRedrawId = $requestAnimationFrame(redraw, FRAME_BUDGET)
}
@ -1830,8 +1814,8 @@
}
function parseQueryString(str) {
if (str === "" || str == null) return {}
if (str.charAt(0) === "?") str = str.slice(1)
if (!str) return {}
if (str[0] === "?") str = str.slice(1)
var pairs = str.split("&")
var params = {}
@ -2136,7 +2120,7 @@
script.src = options.url +
(options.url.indexOf("?") > 0 ? "&" : "?") +
(options.callbackKey ? options.callbackKey : "callback") +
(options.callbackKey || "callback") +
"=" + callbackKey +
"&" + buildQueryString(options.data || {})
@ -2183,7 +2167,7 @@
data = options.data
}
if (data && !isString(data) && data.constructor !== window.FormData) {
if (data && !isString(data) && !(data instanceof window.FormData)) {
throw new Error("Request data should be either be a string or " +
"FormData. Check the `serialize` option in `m.request`")
}
@ -2193,7 +2177,7 @@
}
function ajax(options) {
if (options.dataType && options.dataType.toLowerCase() === "jsonp") {
if (options.dataType === "JSONP") {
return getJsonp(options)
} else {
return runXhr(options)
@ -2235,15 +2219,15 @@
return jsonp.responseText
}
if (!options.dataType || options.dataType.toLowerCase() !== "jsonp") {
if (!options.dataType || options.dataType.toUpperCase() !== "JSONP") {
options.dataType = "JSONP"
serialize = options.serialize || JSON.stringify
deserialize = options.deserialize || JSON.parse
extract = options.extract || function (xhr) {
if (xhr.responseText.length === 0 &&
deserialize === JSON.parse) {
return null
} else {
if (xhr.responseText.length || deserialize !== JSON.parse) {
return xhr.responseText
} else {
return null
}
}
}

2
mithril.min.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long