Advertisement
Guest User

Untitled

a guest
Aug 14th, 2017
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. <html>
  2. <p>
  3. Ispect and view the console to see the result or press <strong> Ctrl + Shift + J (window)</strong>
  4. and <strong> Cmd+ Opt + J(Mac)</strong>
  5. </p>
  6. Do not input more than 60,000 record or your browser will be crashed. <br/>
  7. <input style="width: 300px;" type="number" placeholder="Input Number of records here" id="txtinput">
  8. <input type="button" name="" value="OK" onclick="startExperiment(0)">
  9. <script>
  10.  
  11. // VARIABLE DECLARATION
  12. var studentlist = [];
  13. var classlist = [];
  14. var displaylistNormal = [];
  15. var displaylistHash = [];
  16. var hashClssList = {};
  17. var count_normal_loop = 0;
  18. var count_hash_loop = 0;
  19. var count_if = 0;
  20.  
  21.  
  22. // FUNCTIONS DECLARATION
  23. function createStudentList(n_loop){
  24. studentlist = [];
  25. for(i=0; i<n_loop; i++){
  26. var student = {id:i, name:"studen"+i, classid:i};
  27. studentlist.push(student);
  28. }// end for
  29. }// end function
  30.  
  31. function createClassList(n_loop){
  32. classlist = [];
  33. for(i=0; i<n_loop; i++){
  34. var clss = {id:i, clsname:"class name"+i, date:"date"+i};
  35. classlist.push(clss);
  36. }// for
  37. }// end function
  38.  
  39.  
  40. function normalLoop(){
  41. var n_loop = 0;
  42. for(i=0; i<studentlist.length; i++){
  43. var student = studentlist[i];
  44. for(j=0; j<classlist.length; j++){
  45. var clss = classlist[j];
  46. if(clss.id == student.classid){
  47. displaylistNormal.push({student_name:student.name, class_name:clss.clsname, date: new Date()});
  48. break;
  49. //console.log(displaylist[i]);
  50. }// end if
  51. count_if++;
  52. count_normal_loop++;
  53. }// end for j
  54. }// end for i
  55. }// end funciton
  56.  
  57. function hashloop(){
  58. for(i=0; i<classlist.length; i++){
  59. var clss = classlist[i];
  60. hashClssList[clss.id] = clss;
  61. count_hash_loop++;
  62. }// end for i
  63.  
  64. for (j=0; j<studentlist.length; j++){
  65. var student = studentlist[j];
  66. var clss = hashClssList[student.classid];
  67. displaylistHash.push({student_name:student.name, class_name:clss.clsname, date: new Date()});
  68. count_hash_loop++;
  69. //console.log(displaylist[j]);
  70. }// end for j
  71. }// end function
  72.  
  73. function numberWithCommas(x) {
  74. return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  75. }// end function
  76.  
  77.  
  78. function startExperiment(n_loop){
  79. n_loop = document.getElementById("txtinput").value;
  80. clearConsole();
  81. clearVariables();
  82. // START THE EXPERIMENT
  83. createClassList(n_loop);
  84. createStudentList(n_loop);
  85.  
  86.  
  87. // Criteria
  88. console.log("EXPERIMENT CRITERIA");
  89. console.log("Size of student list: "+numberWithCommas(studentlist.length)+" records");
  90. console.log("Size of class list: "+numberWithCommas(classlist.length)+" records");
  91. console.log("-----------");
  92.  
  93. // Normal Loop with break
  94. var startTime = new Date().getTime();
  95. normalLoop();
  96. var endTime = new Date().getTime();
  97. console.log("Normal Loop");
  98. console.log(" time used: "+(endTime-startTime)+"ms");
  99. console.log(" count loop(with break): "+numberWithCommas(count_normal_loop)+" loops");
  100. console.log(" count if: "+numberWithCommas(count_if)+" if statements");
  101. console.log(" count loop (without break): "+numberWithCommas(studentlist.length*classlist.length)+" loops");
  102. console.log(" Result: ");
  103. console.log(displaylistNormal);
  104. console.log("\n");
  105.  
  106. // Hash Loop
  107. startTime = new Date().getTime();
  108. hashloop();
  109. endTime = new Date().getTime();
  110. console.log("HASH LOOP ");
  111. console.log(" time used: "+(endTime-startTime)+"ms");
  112. console.log(" count loop: "+numberWithCommas(count_hash_loop)+" loops");
  113. console.log(" count if: 0 if statements");
  114. console.log(" Result:");
  115. console.log(displaylistHash);
  116. }// end function
  117.  
  118. function clearConsole(){
  119. console.API;
  120.  
  121. if (typeof console._commandLineAPI !== 'undefined') {
  122. console.API = console._commandLineAPI; //chrome
  123. } else if (typeof console._inspectorCommandLineAPI !== 'undefined') {
  124. console.API = console._inspectorCommandLineAPI; //Safari
  125. } else if (typeof console.clear !== 'undefined') {
  126. console.API = console;
  127. }
  128.  
  129. console.API.clear();
  130. }
  131.  
  132. function clearVariables(){
  133. studentlist = [];
  134. classlist = [];
  135. displaylistNormal = [];
  136. displaylistHash = [];
  137. hashClssList = {};
  138. count_normal_loop = 0;
  139. count_hash_loop = 0;
  140. count_if = 0;
  141. }
  142.  
  143.  
  144. </script>
  145. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement