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) { 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,7 @@
case MOVE: case MOVE:
var changeElement = change.element var changeElement = change.element
if (inst.parent.childNodes[index] !== changeElement && if (inst.parent.childNodes[index] !== changeElement) {
changeElement !== null) {
inst.parent.insertBefore( inst.parent.insertBefore(
changeElement, changeElement,
inst.parent.childNodes[index] || null inst.parent.childNodes[index] || null
@ -764,7 +764,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 +790,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 +863,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 +897,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 +999,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) {
@ -1053,10 +1043,9 @@
inst.editable.innerHTML = inst.data inst.editable.innerHTML = inst.data
} else { } else {
// was a trusted string // was a trusted string
if (nodes[0].nodeType === 1 || if (nodes[0].nodeType === 1 || nodes.length > 1 ||
nodes.length > 1 || isString(nodes[0].nodeValue) &&
(nodes[0].nodeValue.trim && !nodes[0].nodeValue.trim()) /\s*/.test(nodes[0].nodeValue)) {
) {
clear(inst.cached.nodes, inst.cached) clear(inst.cached.nodes, inst.cached)
nodes = [$document.createTextNode(inst.data)] nodes = [$document.createTextNode(inst.data)]
} }
@ -1112,12 +1101,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) {
@ -1159,7 +1143,7 @@
// used as a string, but it's an object in js // used as a string, but it's an object in js
// //
// #348 // #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 // breaks in Chrome
if (tag !== "input" || node[attr] !== dataAttr) { if (tag !== "input" || node[attr] !== dataAttr) {
node[attr] = dataAttr node[attr] = dataAttr
@ -1175,7 +1159,7 @@
} catch (e) { } catch (e) {
// swallow IE's invalid argument errors to mimic HTML's // swallow IE's invalid argument errors to mimic HTML's
// fallback-to-doing-nothing-on-invalid-attributes behavior // 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) trySetSingle(attr, dataAttr, cachedAttr, node, namespace, tag)
} else if (attr === "value" && tag === "input" && } else if (attr === "value" && tag === "input" &&
// #348: dataAttr may not be a string, so use loose // #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) { // eslint-disable-line eqeqeq
node.value = dataAttr node.value = dataAttr
} }
@ -1362,7 +1346,7 @@
function getCellCacheKey(element) { function getCellCacheKey(element) {
var index = nodeCache.indexOf(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) { m.trust = function (value) {
@ -1468,7 +1452,7 @@
var lastRedrawCallTime = 0 var lastRedrawCallTime = 0
function actuallyPerformRedraw() { function actuallyPerformRedraw() {
if (lastRedrawId > 0) $cancelAnimationFrame(lastRedrawId) if (lastRedrawId !== 0) $cancelAnimationFrame(lastRedrawId)
lastRedrawId = $requestAnimationFrame(redraw, FRAME_BUDGET) lastRedrawId = $requestAnimationFrame(redraw, FRAME_BUDGET)
} }
@ -1830,8 +1814,8 @@
} }
function parseQueryString(str) { function parseQueryString(str) {
if (str === "" || str == null) return {} if (!str) 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 = {}
@ -2136,7 +2120,7 @@
script.src = options.url + script.src = options.url +
(options.url.indexOf("?") > 0 ? "&" : "?") + (options.url.indexOf("?") > 0 ? "&" : "?") +
(options.callbackKey ? options.callbackKey : "callback") + (options.callbackKey || "callback") +
"=" + callbackKey + "=" + callbackKey +
"&" + buildQueryString(options.data || {}) "&" + buildQueryString(options.data || {})
@ -2183,7 +2167,7 @@
data = options.data 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 " + throw new Error("Request data should be either be a string or " +
"FormData. Check the `serialize` option in `m.request`") "FormData. Check the `serialize` option in `m.request`")
} }
@ -2193,7 +2177,7 @@
} }
function ajax(options) { function ajax(options) {
if (options.dataType && options.dataType.toLowerCase() === "jsonp") { if (options.dataType === "JSONP") {
return getJsonp(options) return getJsonp(options)
} else { } else {
return runXhr(options) return runXhr(options)
@ -2235,15 +2219,15 @@
return jsonp.responseText 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 serialize = options.serialize || JSON.stringify
deserialize = options.deserialize || JSON.parse deserialize = options.deserialize || JSON.parse
extract = options.extract || function (xhr) { extract = options.extract || function (xhr) {
if (xhr.responseText.length === 0 && if (xhr.responseText.length || deserialize !== JSON.parse) {
deserialize === JSON.parse) {
return null
} else {
return xhr.responseText 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