diff --git a/docs/change-log.md b/docs/change-log.md index 7ec773c3..1627c459 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -17,6 +17,7 @@ - set promise's default value to initialValue if coming from m.request [#454](https://github.com/lhorie/mithril.js/issues/454) - fix dom element ownership bug when mixing keyed elements and third party plugin elements [#463](https://github.com/lhorie/mithril.js/issues/463) - fix edge case in flatten algorithm [#448](https://github.com/lhorie/mithril.js/issues/448) +- prevent unnecessary DOM move operation when mixing keyed and unkeyed elements [#398](https://github.com/lhorie/mithril.js/issues/398) --- diff --git a/mithril.js b/mithril.js index f9ab73ee..1a2742df 100644 --- a/mithril.js +++ b/mithril.js @@ -137,6 +137,10 @@ var m = (function app(window, undefined) { existing[cached[i].attrs.key] = {action: DELETION, index: i} } } + var guid = 0 + for (var i = 0, len = data.length; i < len; i++) { + if (data[i] && data[i].attrs && data[i].attrs.key == null) data[i].attrs.key = "__mithril__" + guid++ + } if (shouldMaintainIdentities) { if (data.indexOf(null) > -1) data = data.filter(function(x) {return x != null}) @@ -212,7 +216,7 @@ var m = (function app(window, undefined) { //fix offset of next element if item was a trusted string w/ more than one html element //the first clause in the regexp matches elements //the second clause (after the pipe) matches text nodes - subArrayCount += (item.match(/<[^\/]|\>\s*[^<]|&/g) || []).length + subArrayCount += (item.match(/<[^\/]|\>\s*[^<]/g) || [0]).length } else subArrayCount += type.call(item) === ARRAY ? item.length : 1; cached[cacheCount++] = item