Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from dannguyen/master
Browse files Browse the repository at this point in the history
Minor optimization for for loops; cache length property
  • Loading branch information
jsoma committed Mar 10, 2012
2 parents 117aeba + 51bbef0 commit 13e509d
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/tabletop.js
Expand Up @@ -163,10 +163,10 @@
Used as a callback for the worksheet-based JSON Used as a callback for the worksheet-based JSON
*/ */
loadSheets: function(data) { loadSheets: function(data) {
var i; var i, ilen;
var toInject = []; var toInject = [];


for(i = 0; i < data.feed.entry.length; i++) { for(i = 0, ilen = data.feed.entry.length; i < ilen ; i++) {
// Only pull in desired sheets to reduce loading // Only pull in desired sheets to reduce loading
if( this.isWanted(data.feed.entry[i].content.$t) ) { if( this.isWanted(data.feed.entry[i].content.$t) ) {
var sheet_id = data.feed.entry[i].link[3].href.substr( data.feed.entry[i].link[3].href.length - 3, 3); var sheet_id = data.feed.entry[i].link[3].href.substr( data.feed.entry[i].link[3].href.length - 3, 3);
Expand All @@ -176,7 +176,7 @@
} }


this.sheetsToLoad = toInject.length; this.sheetsToLoad = toInject.length;
for(i = 0; i < toInject.length; i++) { for(i = 0, ilen = toInject.length; i < ilen; i++) {
this.injectScript(toInject[i], this.loadSheet); this.injectScript(toInject[i], this.loadSheet);
} }
}, },
Expand Down Expand Up @@ -243,7 +243,7 @@
Options should be in the format { data: XXX }, with XXX being the list-based worksheet Options should be in the format { data: XXX }, with XXX being the list-based worksheet
*/ */
Tabletop.Model = function(options) { Tabletop.Model = function(options) {
var i, j; var i, j, ilen, jlen;
this.column_names = []; this.column_names = [];
this.name = options.data.feed.title.$t; this.name = options.data.feed.title.$t;
this.elements = []; this.elements = [];
Expand All @@ -253,11 +253,10 @@
this.column_names.push( key.replace("gsx$","") ); this.column_names.push( key.replace("gsx$","") );
} }


for(i = 0; i < options.data.feed.entry.length; i++) { for(i = 0, ilen = options.data.feed.entry.length ; i < ilen; i++) {
var source = options.data.feed.entry[i]; var source = options.data.feed.entry[i];
var element = {}; var element = {};

for(var j = 0, jlen = this.column_names.length; j < jlen ; j++) {
for(var j = 0; j < this.column_names.length; j++) {
var cell = source[ "gsx$" + this.column_names[j] ]; var cell = source[ "gsx$" + this.column_names[j] ];
if(options.parseNumbers && cell.$t !== '' && !isNaN(cell.$t)) if(options.parseNumbers && cell.$t !== '' && !isNaN(cell.$t))
element[ this.column_names[j] ] = +cell.$t; element[ this.column_names[j] ] = +cell.$t;
Expand Down Expand Up @@ -286,10 +285,10 @@
*/ */
toArray: function() { toArray: function() {
var array = [], var array = [],
i, j; i, j, ilen, jlen;
for(i = 0; i < this.elements.length; i++) { for(i = 0, ilen = this.elements.length; i < ilen; i++) {
var row = []; var row = [];
for(j = 0; j < this.column_names.length; j++) { for(j = 0, jlen = this.column_names.length; j < jlen ; j++) {
row.push( this.elements[i][ this.column_names[j] ] ); row.push( this.elements[i][ this.column_names[j] ] );
} }
array.push(row); array.push(row);
Expand Down

0 comments on commit 13e509d

Please sign in to comment.