Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <p>
- Ispect and view the console to see the result or press <strong> Ctrl + Shift + J (window)</strong>
- and <strong> Cmd+ Opt + J(Mac)</strong>
- </p>
- Do not input more than 60,000 record or your browser will be crashed. <br/>
- <input style="width: 300px;" type="number" placeholder="Input Number of records here" id="txtinput">
- <input type="button" name="" value="OK" onclick="startExperiment(0)">
- <script>
- // VARIABLE DECLARATION
- var studentlist = [];
- var classlist = [];
- var displaylistNormal = [];
- var displaylistHash = [];
- var hashClssList = {};
- var count_normal_loop = 0;
- var count_hash_loop = 0;
- var count_if = 0;
- // FUNCTIONS DECLARATION
- function createStudentList(n_loop){
- studentlist = [];
- for(i=0; i<n_loop; i++){
- var student = {id:i, name:"studen"+i, classid:i};
- studentlist.push(student);
- }// end for
- }// end function
- function createClassList(n_loop){
- classlist = [];
- for(i=0; i<n_loop; i++){
- var clss = {id:i, clsname:"class name"+i, date:"date"+i};
- classlist.push(clss);
- }// for
- }// end function
- function normalLoop(){
- var n_loop = 0;
- for(i=0; i<studentlist.length; i++){
- var student = studentlist[i];
- for(j=0; j<classlist.length; j++){
- var clss = classlist[j];
- if(clss.id == student.classid){
- displaylistNormal.push({student_name:student.name, class_name:clss.clsname, date: new Date()});
- break;
- //console.log(displaylist[i]);
- }// end if
- count_if++;
- count_normal_loop++;
- }// end for j
- }// end for i
- }// end funciton
- function hashloop(){
- for(i=0; i<classlist.length; i++){
- var clss = classlist[i];
- hashClssList[clss.id] = clss;
- count_hash_loop++;
- }// end for i
- for (j=0; j<studentlist.length; j++){
- var student = studentlist[j];
- var clss = hashClssList[student.classid];
- displaylistHash.push({student_name:student.name, class_name:clss.clsname, date: new Date()});
- count_hash_loop++;
- //console.log(displaylist[j]);
- }// end for j
- }// end function
- function numberWithCommas(x) {
- return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
- }// end function
- function startExperiment(n_loop){
- n_loop = document.getElementById("txtinput").value;
- clearConsole();
- clearVariables();
- // START THE EXPERIMENT
- createClassList(n_loop);
- createStudentList(n_loop);
- // Criteria
- console.log("EXPERIMENT CRITERIA");
- console.log("Size of student list: "+numberWithCommas(studentlist.length)+" records");
- console.log("Size of class list: "+numberWithCommas(classlist.length)+" records");
- console.log("-----------");
- // Normal Loop with break
- var startTime = new Date().getTime();
- normalLoop();
- var endTime = new Date().getTime();
- console.log("Normal Loop");
- console.log(" time used: "+(endTime-startTime)+"ms");
- console.log(" count loop(with break): "+numberWithCommas(count_normal_loop)+" loops");
- console.log(" count if: "+numberWithCommas(count_if)+" if statements");
- console.log(" count loop (without break): "+numberWithCommas(studentlist.length*classlist.length)+" loops");
- console.log(" Result: ");
- console.log(displaylistNormal);
- console.log("\n");
- // Hash Loop
- startTime = new Date().getTime();
- hashloop();
- endTime = new Date().getTime();
- console.log("HASH LOOP ");
- console.log(" time used: "+(endTime-startTime)+"ms");
- console.log(" count loop: "+numberWithCommas(count_hash_loop)+" loops");
- console.log(" count if: 0 if statements");
- console.log(" Result:");
- console.log(displaylistHash);
- }// end function
- function clearConsole(){
- console.API;
- if (typeof console._commandLineAPI !== 'undefined') {
- console.API = console._commandLineAPI; //chrome
- } else if (typeof console._inspectorCommandLineAPI !== 'undefined') {
- console.API = console._inspectorCommandLineAPI; //Safari
- } else if (typeof console.clear !== 'undefined') {
- console.API = console;
- }
- console.API.clear();
- }
- function clearVariables(){
- studentlist = [];
- classlist = [];
- displaylistNormal = [];
- displaylistHash = [];
- hashClssList = {};
- count_normal_loop = 0;
- count_hash_loop = 0;
- count_if = 0;
- }
- </script>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement