KEMBAR78
JavaScript Loop: Optimization of Weak Typing | PPTX
JavaScript LoopOptimization of weak typingtaibo@alipay.com
AboutJanlay Wux涂料Gtalk: janlay@gmail.comQQ: 629489Twitter: @janlayBlog: janlay.com5dmedia.comSunSourcing.comaabase.comRe1001.comAlipay.com
i++ vs i--for(var i = 0, stub = 0; i < 100000; 	i++) {	stub++;}for(vari = 100000, stub = 0; i > 0; 	i--) {stub++;  }
Which is faster?
Run the codei--IE 8: 41-55 msChrome 5: 3-7 msFirefox 3: 0-2 msOpera 10: 0-1 msi++IE 8: 43-48 msChrome 5: 1-5 msFirefox 3: 0-2 msOpera 10: 0-1 ms
Conclusion?It depends.
Why i–- is not significantly faster?Too Young: No warming upToo Simple: Only one statementAnd, depends on JavaScript Compiler
Make them more complex
1. Define function for Test casesvar test = function(name, fun, times) {// validate timesif(times < 1) throw 'Invalid times';// initvartimers = [new Date()], count = 0, total = 0;// run test casewhile(count++ < times) {	fun();timers[count] = new Date();}// output resultdocument.write("<p>Result for " + name + ":<br/>");for(vari = 1, interval = 0; i <= times; i++) {document.write(i + " - " + (interval = timers[i] - timers[i - 1])  + "ms<br/>");total += interval;}document.write("Average: " + (total / times) + "ms</p>");};
2. Use itvar stub = 0;	test('i--', function() {	  for(var i = 100000; i > 0; i--) {		stub++; // access variable out of closure	  }	}, 10);	test('i++', function() {	  for(var i = 0; i < 100000; i++) {		stub++;	  }	}, 10);
1,000,000
So, we goti--IE 8: 25.4 msChrome 5: 1.8 msFirefox 3: 0.8 msOpera 10: 2.2 msi++IE 8: 27.8 msChrome 5: 0.8 msFirefox 3: 1.0 msOpera 10: 2.0 ms
Just ignore the differences1.0 / 100,000 / 1,000 => 1/100,000,000 (s)
… …
Typing!JavaScript is quite different from Java/C
JavaScript is quite different from Java/CStrong Typing vs Weak TypingStatic Typing vs Dynamic Typing
Strong Typing vs Weak TypingC: short, int, long, float…1.0fvs1++ designed for CPU instruction INC++ => INC, -- => DEC> 0 => JNZ (only i--, non-float)< 1 => SUBB, JNCJavaScript: Number1.0 ~ ’1’++ inherits from C-style languages++ equals +1, mostly
Static Typing vsDynamic TypingC: types determined in compile timeMay be benefit for optimizationJIT on VM(msvcrt, JVM, .NET…)JavaScript: types determined in run timeBasedon VM, cannot use CPU instructions directly
Finally…Consider business logici--: order-independentFocus on the most important partyou know it
ThanksQ & A

JavaScript Loop: Optimization of Weak Typing

  • 1.
    JavaScript LoopOptimization ofweak typingtaibo@alipay.com
  • 2.
    AboutJanlay Wux涂料Gtalk: janlay@gmail.comQQ:629489Twitter: @janlayBlog: janlay.com5dmedia.comSunSourcing.comaabase.comRe1001.comAlipay.com
  • 3.
    i++ vs i--for(vari = 0, stub = 0; i < 100000; i++) { stub++;}for(vari = 100000, stub = 0; i > 0; i--) {stub++; }
  • 4.
  • 5.
    Run the codei--IE8: 41-55 msChrome 5: 3-7 msFirefox 3: 0-2 msOpera 10: 0-1 msi++IE 8: 43-48 msChrome 5: 1-5 msFirefox 3: 0-2 msOpera 10: 0-1 ms
  • 6.
  • 7.
    Why i–- isnot significantly faster?Too Young: No warming upToo Simple: Only one statementAnd, depends on JavaScript Compiler
  • 8.
  • 9.
    1. Define functionfor Test casesvar test = function(name, fun, times) {// validate timesif(times < 1) throw 'Invalid times';// initvartimers = [new Date()], count = 0, total = 0;// run test casewhile(count++ < times) { fun();timers[count] = new Date();}// output resultdocument.write("<p>Result for " + name + ":<br/>");for(vari = 1, interval = 0; i <= times; i++) {document.write(i + " - " + (interval = timers[i] - timers[i - 1]) + "ms<br/>");total += interval;}document.write("Average: " + (total / times) + "ms</p>");};
  • 10.
    2. Use itvarstub = 0; test('i--', function() { for(var i = 100000; i > 0; i--) { stub++; // access variable out of closure } }, 10); test('i++', function() { for(var i = 0; i < 100000; i++) { stub++; } }, 10);
  • 11.
  • 12.
    So, we goti--IE8: 25.4 msChrome 5: 1.8 msFirefox 3: 0.8 msOpera 10: 2.2 msi++IE 8: 27.8 msChrome 5: 0.8 msFirefox 3: 1.0 msOpera 10: 2.0 ms
  • 13.
    Just ignore thedifferences1.0 / 100,000 / 1,000 => 1/100,000,000 (s)
  • 14.
  • 15.
    Typing!JavaScript is quitedifferent from Java/C
  • 16.
    JavaScript is quitedifferent from Java/CStrong Typing vs Weak TypingStatic Typing vs Dynamic Typing
  • 17.
    Strong Typing vsWeak TypingC: short, int, long, float…1.0fvs1++ designed for CPU instruction INC++ => INC, -- => DEC> 0 => JNZ (only i--, non-float)< 1 => SUBB, JNCJavaScript: Number1.0 ~ ’1’++ inherits from C-style languages++ equals +1, mostly
  • 18.
    Static Typing vsDynamicTypingC: types determined in compile timeMay be benefit for optimizationJIT on VM(msvcrt, JVM, .NET…)JavaScript: types determined in run timeBasedon VM, cannot use CPU instructions directly
  • 19.
    Finally…Consider business logici--:order-independentFocus on the most important partyou know it
  • 20.