nested pad2()

JavaScript performance comparison

Test case created by Tommy-Carlos Williams

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
nested
(function(){
 function PlaneTicket(name, flight, departure_time) {
   // The name of the person on the ticket
   this.name = name;
   // The airline and flight number, i.e. "AA 362"
   this.flight = flight;
   // The departure time - a Date object
   this.departure_time = departure_time;
 }

 // GMT date formatted as YYYY-MM-DD HH:MM
 PlaneTicket.prototype.getGMTDate = function(inputDate) {
   var pad2 = function(num) {
     return (num < 10 ? '0' : '') + num;
   };
   var formattedDate =
     inputDate.getUTCFullYear() + "-" +
     pad2(inputDate.getUTCMonth() + 1) + "-" +
     pad2(inputDate.getUTCDate()) + " " +
     pad2(inputDate.getUTCHours()) + ":" +
     pad2(inputDate.getUTCMinutes()) + ":" +
     pad2(inputDate.getUTCSeconds());
   return formattedDate;
 };


 var tickets = [
   new PlaneTicket('John Smith', 'AA 443', new Date('December 24, 2012 3:14 CDT')),
   new PlaneTicket('Amy Johnson', 'Delta 219', new Date('December 23, 2012 18:22 EDT')),
   new PlaneTicket('George Fairbanks', 'Southwest 338', new Date('December 14, 2012 11:45 PDT')),
   new PlaneTicket('Aki Yamamura', 'JAL 117', new Date('December 18, 2012 17:02 UTC+9:00'))
 ];

 // Sort by departure_time
 tickets.sort(function(a,b){
   return parseInt(a.departure_time.getTime(),10) - parseInt(b.departure_time.getTime(),10);
 });

 // Output
 var ticketsLength = tickets.length;
 for (var i = 0;i<tickets.length;i++) {
   console.log(tickets[i].name + ", " + tickets[i].getGMTDate(tickets[i].departure_time));
 }
})();
pending…
not nested
(function(){
 function PlaneTicket(name, flight, departure_time) {
   // The name of the person on the ticket
   this.name = name;
   // The airline and flight number, i.e. "AA 362"
   this.flight = flight;
   // The departure time - a Date object
   this.departure_time = departure_time;
 }

 var pad2 = function(num) {
   return (num < 10 ? '0' : '') + num;
 };

 // GMT date formatted as YYYY-MM-DD HH:MM
 PlaneTicket.prototype.getGMTDate = function(inputDate) {
   var formattedDate =
     inputDate.getUTCFullYear() + "-" +
     pad2(inputDate.getUTCMonth() + 1) + "-" +
     pad2(inputDate.getUTCDate()) + " " +
     pad2(inputDate.getUTCHours()) + ":" +
     pad2(inputDate.getUTCMinutes()) + ":" +
     pad2(inputDate.getUTCSeconds());
   return formattedDate;
 };


 var tickets = [
   new PlaneTicket('John Smith', 'AA 443', new Date('December 24, 2012 3:14 CDT')),
   new PlaneTicket('Amy Johnson', 'Delta 219', new Date('December 23, 2012 18:22 EDT')),
   new PlaneTicket('George Fairbanks', 'Southwest 338', new Date('December 14, 2012 11:45 PDT')),
   new PlaneTicket('Aki Yamamura', 'JAL 117', new Date('December 18, 2012 17:02 UTC+9:00'))
 ];

 // Sort by departure_time
 tickets.sort(function(a,b){
   return parseInt(a.departure_time.getTime(),10) - parseInt(b.departure_time.getTime(),10);
 });

 // Output
 var ticketsLength = tickets.length;
 for (var i = 0;i<tickets.length;i++) {
   console.log(tickets[i].name + ", " + tickets[i].getGMTDate(tickets[i].departure_time));
 }
})();
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment