Created
May 13, 2013 22:14
-
-
Save lyle/5571964 to your computer and use it in GitHub Desktop.
Testing speed on Arrays in JS client side - I was curious if calling instanceOfArray.length was slower then referencing an intager.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var smallArray = [4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,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3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5,4,5,6,7,8,9,4,22,3,5]; | |
| function lengthPer(inArray){ | |
| var i=0; | |
| for(i=0;i<inArray.length;i++){ | |
| } | |
| return i; | |
| } | |
| function lengthOnce(inArray){ | |
| var i=0; | |
| var leng = inArray.length; | |
| for(i=0;i<leng;i++){ | |
| } | |
| return i; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <title>Testing Array Length Access Speed</title> | |
| <script type="text/javascript" src="arrayLengthTest.js"></script> | |
| </head> | |
| <body> | |
| <div id="out"></div> | |
| <button id="doIt">Do It</button> | |
| <script type="text/javascript"> | |
| document.getElementById('doIt').addEventListener('click',function(){testSpeed();},false); | |
| function testSpeed() { | |
| var parent=0; | |
| var start1, end1, start2, end2; | |
| start1 = Date.now(); | |
| for(parent=0;parent<1000000;parent++){ | |
| lengthPer(smallArray); | |
| } | |
| end1 = Date.now(); | |
| start2 = Date.now(); | |
| for(parent=0;parent<1000000;parent++){ | |
| lengthOnce(smallArray); | |
| } | |
| end2 = Date.now(); | |
| alert("length called each loop=" + (end1 - start1) + "\nlength called once=" + (end2 - start2) ); | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment