JS Code Execution Time


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
  2. <script type="text/javascript">
  3.  
  4. $(document).ready(function(){
  5.  
  6. var str = document.getElementById('txt').value,
  7. n = [],
  8. startTime = new Date().getTime(),
  9. endTime = 0,
  10. totalTime = 0,
  11. l = 0,
  12. i = 0;
  13.  
  14. /*
  15. simple method that iterates over a string and adds
  16. each char to an array so see how fast it is.
  17. */
  18. function go() {
  19.  
  20. for (i=0,l=str.length; i<l; i++) {
  21. n.push(str.charAt(i));
  22. }
  23.  
  24. endTime = new Date().getTime();
  25.  
  26. }
  27.  
  28. go();
  29.  
  30. totalTime = endTime - startTime;
  31.  
  32.  
  33. $("#time").html("<p>Time: "+totalTime+"ms</p>");
  34. });
  35. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.