From 8f63e2a959a7ac44533ae2b6192716ee17a1ce93 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Wed, 8 Jul 2015 08:27:28 +0100 Subject: [PATCH] Optimise iterating through tiddlers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Astonishingly, it’s much quicker to use `Object.keys()` to get an array of key names, and then iterate through that. I’m seeing 25% speed improvements for an empty tiddler iterator. --- boot/boot.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/boot/boot.js b/boot/boot.js index 016672278..58d1376ac 100644 --- a/boot/boot.js +++ b/boot/boot.js @@ -910,7 +910,10 @@ $tw.Wiki = function(options) { // Iterate through all tiddler titles this.each = function(callback) { - for(var title in tiddlers) { + var titles = Object.keys(tiddlers), + index,titlesLength,title; + for(index = 0, titlesLength = titles.length; index < titlesLength; index++) { + title = titles[index]; callback(tiddlers[title],title); } }; @@ -922,7 +925,10 @@ $tw.Wiki = function(options) { // Iterate through all shadow tiddler titles this.eachShadow = function(callback) { - for(var title in shadowTiddlers) { + var titles = Object.keys(shadowTiddlers), + index,titlesLength,title; + for(index = 0, titlesLength = titles.length; index < titlesLength; index++) { + title = titles[index]; var shadowInfo = shadowTiddlers[title]; callback(shadowInfo.tiddler,title); } @@ -930,10 +936,15 @@ $tw.Wiki = function(options) { // Iterate through all tiddlers and then the shadows this.eachTiddlerPlusShadows = function(callback) { - for(var title in tiddlers) { + var titles = Object.keys(tiddlers), + index,titlesLength,title; + for(index = 0, titlesLength = titles.length; index < titlesLength; index++) { + title = titles[index]; callback(tiddlers[title],title); } - for(var title in shadowTiddlers) { + titles = Object.keys(shadowTiddlers); + for(index = 0, titlesLength = titles.length; index < titlesLength; index++) { + title = titles[index]; if(!Object.prototype.hasOwnProperty.call(tiddlers,title)) { var shadowInfo = shadowTiddlers[title]; callback(shadowInfo.tiddler,title); @@ -943,7 +954,10 @@ $tw.Wiki = function(options) { // Iterate through all the shadows and then the tiddlers this.eachShadowPlusTiddlers = function(callback) { - for(var title in shadowTiddlers) { + var titles = Object.keys(shadowTiddlers), + index,titlesLength,title; + for(index = 0, titlesLength = titles.length; index < titlesLength; index++) { + title = titles[index]; if(Object.prototype.hasOwnProperty.call(tiddlers,title)) { callback(tiddlers[title],title); } else { @@ -951,7 +965,9 @@ $tw.Wiki = function(options) { callback(shadowInfo.tiddler,title); } } - for(var title in tiddlers) { + titles = Object.keys(tiddlers); + for(index = 0, titlesLength = titles.length; index < titlesLength; index++) { + title = titles[index]; if(!Object.prototype.hasOwnProperty.call(shadowTiddlers,title)) { callback(tiddlers[title],title); }