Commits

Chris Nanney committed 196af6b

Optimizing splitToArray code, reducing length calculations in digitCheck.

Comments (0)

Files changed (2)

js/flipcounter.js

 /**
  * Apple-Style Flip Counter
  * ------------------------
- * Version 0.5 - April 13, 2011 
+ * Version 0.5.1 - April 13, 2011 
  *
  * Copyright (c) 2010 Chris Nanney
  * http://cnanney.com/journal/code/apple-style-counter-revisited/
 	}
 	
 	function digitCheck(x,y){
-		var diff, adder,
-		x = x.toString(),
-		y = y.toString();
 		digitsOld = splitToArray(x);
 		digitsNew = splitToArray(y);
-		if (y.length > x.length){
-			diff = y.length - x.length;
+		var diff,
+		xlen = digitsOld.length,
+		ylen = digitsNew.length;
+		if (ylen > xlen){
+			diff = ylen - xlen;
 			while (diff > 0){
-				adder = 1;
-				addDigit(y.length - diff + 1, digitsNew[y.length - diff]);
-				adder++;
+				addDigit(ylen - diff + 1, digitsNew[ylen - diff]);
 				diff--;
 			}
 		}
-		if (y.length < x.length){
-			diff = x.length - y.length;
+		if (ylen < xlen){
+			diff = xlen - ylen;
 			while (diff > 0){
-				adder = 1;
-				removeDigit(x.length - diff);
+				removeDigit(xlen - diff);
 				diff--;
 			}
 		}
-		for (var i = 0; i < digitsOld.length; i++){
+		for (var i = 0; i < xlen; i++){
 			if (digitsNew[i] != digitsOld[i]){
 				animateDigit(i, digitsOld[i], digitsNew[i]);
 			}
 	
 	// Creates array of digits for easier manipulation
 	function splitToArray(input){
-		var digits = new Array();
-		for (var i = 0; i < input.length; i++){
-			subStart = input.length - (i + 1);
-			subEnd = input.length - i;
-			digits[i] = input.substring(subStart, subEnd);
-		}
-		return digits;
+		return input.toString().split("").reverse();
 	}
 
 	// Adds new digit

js/flipcounter.min.js

 /**
  * Apple-Style Flip Counter
- * Version 0.5 - April 13, 2011 
+ * Version 0.5.1 - April 13, 2011 
  *
  * Copyright (c) 2010 Chris Nanney
  * http://cnanney.com/journal/code/apple-style-counter-revisited/
  * Licensed under MIT
  * http://www.opensource.org/licenses/mit-license.php
  */
-var flipCounter=function(w,D){function s(){l=a.value;a.value+=a.inc;h=a.value;q(l,h);a.auto===!0&&(m=setTimeout(s,a.pace))}function x(c,e,b){var d=a.value,f=typeof e=="undefined"?!1:e,g=typeof b=="undefined"?1:b;f===!0&&g--;d!=c?(l=a.value,a.auto=!0,d+a.inc<=c&&g!=0?d+=a.inc:d=c,a.value=d,h=a.value,q(l,h),m=setTimeout(function(){x(c,f,g)},a.pace)):a.auto=!1}function q(c,e){var b,c=c.toString(),e=e.toString();t=y(c);u=y(e);if(e.length>c.length)for(b=e.length-c.length;b>0;){var d=u[e.length-b],f=Number(e.length-
-b+1)-1;i=k.createElement("ul");i.className="cd";i.id=a.idPre+"_d"+f;i.innerHTML='<li class="t" id="'+a.idPre+"_t_d"+f+'"></li><li class="b" id="'+a.idPre+"_b_d"+f+'"></li>';if(f%3==0)n=k.createElement("ul"),n.className="cd",n.innerHTML='<li class="s"></li>',j.insertBefore(n,j.firstChild);j.insertBefore(i,j.firstChild);k.getElementById(a.idPre+"_t_d"+f).style.backgroundPosition="0 -"+d*a.tFH+"px";k.getElementById(a.idPre+"_b_d"+f).style.backgroundPosition="0 -"+d*a.bFH+"px";b--}if(e.length<c.length)for(b=
-c.length-e.length;b>0;){d=k.getElementById(a.idPre+"_d"+(c.length-b));j.removeChild(d);d=j.firstChild.firstChild;if((" "+d.className+" ").indexOf(" s ")>-1)d=d.parentNode,j.removeChild(d);b--}for(b=0;b<t.length;b++)u[b]!=t[b]&&E(b,t[b],u[b])}function E(c,e,b){function d(){if(g<7){h=g<3?"t":"b";if(i=k.getElementById(a.idPre+"_"+h+"_d"+c))i.style.backgroundPosition=j[g];g++;g!=3?setTimeout(d,f):d()}}var f,g=0,h,i,j=["-"+a.fW+"px -"+e*a.tFH+"px",a.fW*-2+"px -"+e*a.tFH+"px","0 -"+b*a.tFH+"px","-"+a.fW+
-"px -"+e*a.bFH+"px",a.fW*-2+"px -"+b*a.bFH+"px",a.fW*-3+"px -"+b*a.bFH+"px","0 -"+b*a.bFH+"px"];if(a.auto===!0&&a.pace<=300)switch(c){case 0:f=a.pace/6;break;case 1:f=a.pace/5;break;case 2:f=a.pace/4;break;case 3:f=a.pace/3;break;default:f=a.pace/1.5}else f=80;f=f>80?80:f;d()}function y(a){for(var e=[],b=0;b<a.length;b++)A=a.length-(b+1),B=a.length-b,e[b]=a.substring(A,B);return e}function C(a,e,b,d,f){var g={result:!0};g.cond1=a/e>=1?!0:!1;g.cond2=e*b<=a?!0:!1;g.cond3=Math.abs(e*b-a)<=10?!0:!1;g.cond4=
-Math.abs(e*d-f)<=100?!0:!1;g.cond5=e*d<=f?!0:!1;if(g.cond1&&g.cond2&&g.cond4&&g.cond5){a=Math.abs(a-e*b)+Math.abs(e*d-f);if(o.q===null)o.q=a;if(a<=o.q)o.pace=d,o.inc=b}for(b=1;b<=5;b++)if(g["cond"+b]===!1)g.result=!1;return g}function p(a){return!isNaN(parseFloat(a))&&isFinite(a)}function z(){clearTimeout(m);m=null}var v={value:0,inc:1,pace:1E3,auto:!0,tFH:39,bFH:64,fW:53,idPre:"counter"},a=D||{},k=window.document,j=w&&w!=""?k.getElementById(w):k.getElementById("counter"),r;for(r in v)a[r]=r in a?
-a[r]:v[r];var t=[],u=[],A,B,l,h,m=null,i,n,o={q:null,pace:0,inc:0};this.setValue=function(c){if(p(c))l=a.value,h=c,a.value=c,q(l,h);return this};this.setIncrement=function(c){a.inc=p(c)?c:v.inc;return this};this.setPace=function(c){a.pace=p(c)?c:v.pace;return this};this.setAuto=function(c){if(c&&!a.atuo)a.auto=!0,s();if(!c&&a.auto)m&&z(),a.auto=!1;return this};this.step=function(){a.auto||s();return this};this.add=function(c){if(p(c))l=a.value,a.value+=c,h=a.value,q(l,h);return this};this.subtract=
-function(c){if(p(c))l=a.value,a.value-=c,a.value>=0?h=a.value:(h="0",a.value=0),q(l,h);return this};this.incrementTo=function(c,e,b){m&&z();if(typeof e!="undefined"){var e=p(e)?e*1E3:1E4,b=typeof b!="undefined"&&p(b)?b:a.pace,d=typeof c!="undefined"&&p(c)?c-a.value:0,f,g,h,i=0;o.q=null;b=e/d>b?Math.round(e/d/10)*10:b;f=Math.floor(e/b);g=Math.floor(d/f);h=C(d,f,g,b,e);if(d>0){for(;h.result===!1&&i<100;)b+=10,f=Math.floor(e/b),g=Math.floor(d/f),h=C(d,f,g,b,e),i++;i==100?(a.inc=o.inc,a.pace=o.pace):
-(a.inc=g,a.pace=b);x(c,!0,f)}}else x(c)};this.getValue=function(){return a.value};this.stop=function(){m&&z();return this};(function(c){var c=c.toString(),e=c.length,b=1,d;for(d=0;d<e;d++){i=k.createElement("ul");i.className="cd";i.id=a.idPre+"_d"+d;i.innerHTML=i.innerHTML='<li class="t" id="'+a.idPre+"_t_d"+d+'"></li><li class="b" id="'+a.idPre+"_b_d"+d+'"></li>';j.insertBefore(i,j.firstChild);if(b!=e&&b%3==0)n=k.createElement("ul"),n.className="cd",n.innerHTML='<li class="s"></li>',j.insertBefore(n,
-j.firstChild);b++}c=y(c);for(d=0;d<e;d++)k.getElementById(a.idPre+"_t_d"+d).style.backgroundPosition="0 -"+c[d]*a.tFH+"px",k.getElementById(a.idPre+"_b_d"+d).style.backgroundPosition="0 -"+c[d]*a.bFH+"px";a.auto===!0&&(m=setTimeout(s,a.pace))})(a.value)};
+vvar flipCounter=function(x,B){function u(){l=a.value;a.value+=a.inc;h=a.value;r(l,h);a.auto===!0&&(n=setTimeout(u,a.pace))}function y(d,f,b){var c=a.value,e=typeof f=="undefined"?!1:f,g=typeof b=="undefined"?1:b;e===!0&&g--;c!=d?(l=a.value,a.auto=!0,c+a.inc<=d&&g!=0?c+=a.inc:c=d,a.value=c,h=a.value,r(l,h),n=setTimeout(function(){y(d,e,g)},a.pace)):a.auto=!1}function r(d,f){v=d.toString().split("").reverse();s=f.toString().split("").reverse();var b,c=v.length,e=s.length;if(e>c)for(b=e-c;b>0;){var g=
+s[e-b],m=Number(e-b+1)-1;i=j.createElement("ul");i.className="cd";i.id=a.idPre+"_d"+m;i.innerHTML='<li class="t" id="'+a.idPre+"_t_d"+m+'"></li><li class="b" id="'+a.idPre+"_b_d"+m+'"></li>';if(m%3==0)o=j.createElement("ul"),o.className="cd",o.innerHTML='<li class="s"></li>',k.insertBefore(o,k.firstChild);k.insertBefore(i,k.firstChild);j.getElementById(a.idPre+"_t_d"+m).style.backgroundPosition="0 -"+g*a.tFH+"px";j.getElementById(a.idPre+"_b_d"+m).style.backgroundPosition="0 -"+g*a.bFH+"px";b--}if(e<
+c)for(b=c-e;b>0;){e=j.getElementById(a.idPre+"_d"+(c-b));k.removeChild(e);e=k.firstChild.firstChild;if((" "+e.className+" ").indexOf(" s ")>-1)e=e.parentNode,k.removeChild(e);b--}for(b=0;b<c;b++)s[b]!=v[b]&&C(b,v[b],s[b])}function C(d,f,b){function c(){if(g<7){m=g<3?"t":"b";if(h=j.getElementById(a.idPre+"_"+m+"_d"+d))h.style.backgroundPosition=i[g];g++;g!=3?setTimeout(c,e):c()}}var e,g=0,m,h,i=["-"+a.fW+"px -"+f*a.tFH+"px",a.fW*-2+"px -"+f*a.tFH+"px","0 -"+b*a.tFH+"px","-"+a.fW+"px -"+f*a.bFH+"px",
+a.fW*-2+"px -"+b*a.bFH+"px",a.fW*-3+"px -"+b*a.bFH+"px","0 -"+b*a.bFH+"px"];if(a.auto===!0&&a.pace<=300)switch(d){case 0:e=a.pace/6;break;case 1:e=a.pace/5;break;case 2:e=a.pace/4;break;case 3:e=a.pace/3;break;default:e=a.pace/1.5}else e=80;e=e>80?80:e;c()}function A(a,f,b,c,e){var g={result:!0};g.cond1=a/f>=1?!0:!1;g.cond2=f*b<=a?!0:!1;g.cond3=Math.abs(f*b-a)<=10?!0:!1;g.cond4=Math.abs(f*c-e)<=100?!0:!1;g.cond5=f*c<=e?!0:!1;if(g.cond1&&g.cond2&&g.cond4&&g.cond5){a=Math.abs(a-f*b)+Math.abs(f*c-e);
+if(p.q===null)p.q=a;if(a<=p.q)p.pace=c,p.inc=b}for(b=1;b<=5;b++)if(g["cond"+b]===!1)g.result=!1;return g}function q(a){return!isNaN(parseFloat(a))&&isFinite(a)}function z(){clearTimeout(n);n=null}var w={value:0,inc:1,pace:1E3,auto:!0,tFH:39,bFH:64,fW:53,idPre:"counter"},a=B||{},j=window.document,k=x&&x!=""?j.getElementById(x):j.getElementById("counter"),t;for(t in w)a[t]=t in a?a[t]:w[t];var v=[],s=[],l,h,n=null,i,o,p={q:null,pace:0,inc:0};this.setValue=function(d){if(q(d))l=a.value,h=d,a.value=d,
+r(l,h);return this};this.setIncrement=function(d){a.inc=q(d)?d:w.inc;return this};this.setPace=function(d){a.pace=q(d)?d:w.pace;return this};this.setAuto=function(d){if(d&&!a.atuo)a.auto=!0,u();if(!d&&a.auto)n&&z(),a.auto=!1;return this};this.step=function(){a.auto||u();return this};this.add=function(d){if(q(d))l=a.value,a.value+=d,h=a.value,r(l,h);return this};this.subtract=function(d){if(q(d))l=a.value,a.value-=d,a.value>=0?h=a.value:(h="0",a.value=0),r(l,h);return this};this.incrementTo=function(d,
+f,b){n&&z();if(typeof f!="undefined"){var f=q(f)?f*1E3:1E4,b=typeof b!="undefined"&&q(b)?b:a.pace,c=typeof d!="undefined"&&q(d)?d-a.value:0,e,g,h,i=0;p.q=null;b=f/c>b?Math.round(f/c/10)*10:b;e=Math.floor(f/b);g=Math.floor(c/e);h=A(c,e,g,b,f);if(c>0){for(;h.result===!1&&i<100;)b+=10,e=Math.floor(f/b),g=Math.floor(c/e),h=A(c,e,g,b,f),i++;i==100?(a.inc=p.inc,a.pace=p.pace):(a.inc=g,a.pace=b);y(d,!0,e)}}else y(d)};this.getValue=function(){return a.value};this.stop=function(){n&&z();return this};(function(d){var d=
+d.toString(),f=d.length,b=1,c;for(c=0;c<f;c++){i=j.createElement("ul");i.className="cd";i.id=a.idPre+"_d"+c;i.innerHTML=i.innerHTML='<li class="t" id="'+a.idPre+"_t_d"+c+'"></li><li class="b" id="'+a.idPre+"_b_d"+c+'"></li>';k.insertBefore(i,k.firstChild);if(b!=f&&b%3==0)o=j.createElement("ul"),o.className="cd",o.innerHTML='<li class="s"></li>',k.insertBefore(o,k.firstChild);b++}d=d.toString().split("").reverse();for(c=0;c<f;c++)j.getElementById(a.idPre+"_t_d"+c).style.backgroundPosition="0 -"+d[c]*
+a.tFH+"px",j.getElementById(a.idPre+"_b_d"+c).style.backgroundPosition="0 -"+d[c]*a.bFH+"px";a.auto===!0&&(n=setTimeout(u,a.pace))})(a.value)};