From 7a184cb3b7f7bb992de6522808ca1c3dd37c9e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=CC=81r=20O=CC=88rlygsson?= Date: Thu, 18 Feb 2016 18:42:26 +0000 Subject: [PATCH 1/5] Use RegExp#test instead of String#match Using `.test()` is both faster and more explicit. --- mithril.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mithril.js b/mithril.js index d023ac34..7d3721c8 100644 --- a/mithril.js +++ b/mithril.js @@ -456,7 +456,7 @@ nodes = injectHTML(parentElement, index, data) } else { nodes = [$document.createTextNode(data)] - if (!parentElement.nodeName.match(voidElements)) { + if (!voidElements.test(parentElement.nodeName)) { insertNode(parentElement, nodes[0], index) } } @@ -1902,7 +1902,7 @@ m.deferred.onerror = function (e) { if (type.call(e) === "[object Error]" && - !e.constructor.toString().match(/ Error/)) { + !/ Error/.test(e.constructor.toString())) { pendingRequests = 0 throw e } From 74062cda29ccf1e161b3a9f415bc9ed4592eca39 Mon Sep 17 00:00:00 2001 From: mar Date: Thu, 18 Feb 2016 18:53:45 +0000 Subject: [PATCH 2/5] Parameterize URLs with a single String#replace --- mithril.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/mithril.js b/mithril.js index 7d3721c8..5106d3b7 100644 --- a/mithril.js +++ b/mithril.js @@ -2048,16 +2048,14 @@ } function parameterizeUrl(url, data) { - var tokens = url.match(/:[a-z]\w+/gi) - - if (tokens && data) { - forEach(tokens, function (token) { + if (data) { + url = url.replace(/:[a-z]\w+/gi, function(token){ var key = token.slice(1) - url = url.replace(token, data[key]) + var value = data[key] delete data[key] + return value }) } - return url } From 3e4af6ff221947dd5bc244cbf08c5beac37af624 Mon Sep 17 00:00:00 2001 From: mar Date: Thu, 18 Feb 2016 19:08:07 +0000 Subject: [PATCH 3/5] Use Array#slice to convert arguments to Array --- mithril.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/mithril.js b/mithril.js index 5106d3b7..e30f9cae 100644 --- a/mithril.js +++ b/mithril.js @@ -126,9 +126,7 @@ * or splat (optional) */ function m(tag, pairs) { - for (var args = [], i = 1; i < arguments.length; i++) { - args[i - 1] = arguments[i] - } + var args = [].slice.call(arguments, 1) if (isObject(tag)) return parameterize(tag, args) @@ -1298,9 +1296,7 @@ } m.component = function (component) { - for (var args = [], i = 1; i < arguments.length; i++) { - args.push(arguments[i]) - } + var args = [].slice.call(arguments, 1) return parameterize(component, args) } From 01ff1a36fd40199505d816752f09151256a75739 Mon Sep 17 00:00:00 2001 From: mar Date: Thu, 18 Feb 2016 19:47:38 +0000 Subject: [PATCH 4/5] Use object lookup instead of long RegExp Should be faster. --- mithril.js | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/mithril.js b/mithril.js index e30f9cae..01896d7c 100644 --- a/mithril.js +++ b/mithril.js @@ -38,9 +38,24 @@ function noop() {} - /* eslint-disable max-len */ - var voidElements = /^(AREA|BASE|BR|COL|COMMAND|EMBED|HR|IMG|INPUT|KEYGEN|LINK|META|PARAM|SOURCE|TRACK|WBR)$/ - /* eslint-enable max-len */ + var voidElements = { + AREA: 1, + BASE: 1, + BR: 1, + COL: 1, + COMMAND: 1, + EMBED: 1, + HR: 1, + IMG: 1, + INPUT: 1, + KEYGEN: 1, + LINK: 1, + META: 1, + PARAM: 1, + SOURCE: 1, + TRACK: 1, + WBR: 1 + } // caching commonly used variables var $document, $location, $requestAnimationFrame, $cancelAnimationFrame @@ -454,7 +469,7 @@ nodes = injectHTML(parentElement, index, data) } else { nodes = [$document.createTextNode(data)] - if (!voidElements.test(parentElement.nodeName)) { + if (!(parentElement.nodeName in voidElements)) { insertNode(parentElement, nodes[0], index) } } From 3f097135832fdb58301e825923ead04d2feeed32 Mon Sep 17 00:00:00 2001 From: mar Date: Thu, 18 Feb 2016 19:52:39 +0000 Subject: [PATCH 5/5] Use object lookup instead of long condition chain --- mithril.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mithril.js b/mithril.js index 01896d7c..30cf3714 100644 --- a/mithril.js +++ b/mithril.js @@ -983,13 +983,13 @@ } } - function shouldUseSetAttribute(attrName) { - return attrName !== "list" && - attrName !== "style" && - attrName !== "form" && - attrName !== "type" && - attrName !== "width" && - attrName !== "height" + var shouldUseSetAttribute = { + list: 1, + style: 1, + form: 1, + type: 1, + width: 1, + height: 1 } function setSingleAttr( @@ -1020,7 +1020,7 @@ attrName === "className" ? "class" : attrName, dataAttr) } - } else if (attrName in node && shouldUseSetAttribute(attrName)) { + } else if (attrName in node && !shouldUseSetAttribute[attrName]) { // handle cases that are properties (but ignore cases where we // should use setAttribute instead) //