Skip to content

Instantly share code, notes, and snippets.

@ayxos
Created December 1, 2016 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ayxos/94026b4487c1353211c59ba73e86964b to your computer and use it in GitHub Desktop.
Save ayxos/94026b4487c1353211c59ba73e86964b to your computer and use it in GitHub Desktop.
Testing Performance Comparing Object, If-Else and Switch on Chrome
var dimensionSize = {
mma: { width: 300, height: 50 },
medrect: { width: 300, height: 250 },
xxlarge: { width: 320, height: 50 },
xlarge: { width: 300, height: 50 },
sky: { width: 120, height: 600 },
widesky: { width: 160, height: 600 },
leader: { width: 728, height: 90 },
full_320x480: { width: 320, height: 480 },
full_480x320: { width: 480, height: 320 },
full_640x960: { width: 640, height: 960 },
full_960x640: { width: 960, height: 640 },
full_1136x640: { width: 1136, height: 640 },
full_768x1024: { width: 768, height: 1024 },
full_1024x768: { width: 1024, height: 768 },
full_800x1280: { width: 800, height: 1280 },
small: { width: 120, height: 20 },
large: { width: 216, height: 36 },
medium: { width: 168, height: 68 }
};
function sizeForDimension(dimension) {
var size = {};
if (dimension == "mma") {
size.width = "300px";
size.height = "50px";
} else if (dimension == "medrect") {
size.width = "300px";
size.height = "250px";
} else if (dimension == "xxlarge") {
size.width = "320px";
size.height = "50px";
} else if (dimension == "xlarge") {
size.width = "300px";
size.height = "50px";
} else if (dimension == "sky") {
size.width = "120px";
size.height = "600px";
} else if (dimension == "widesky") {
size.width = "160px";
size.height = "600px";
} else if (dimension == "leader") {
size.width = "728px";
size.height = "90px";
} else if (dimension == "full_320x480") {
size.width = "320px";
size.height = "480px";
} else if (dimension == "full_480x320") {
size.width = "480px";
size.height = "320px";
} else if (dimension == "full_640x960") {
size.width = "640px";
size.height = "960px";
} else if (dimension == "full_960x640") {
size.width = "960px";
size.height = "640px";
} else if (dimension == "full_1136x640") {
size.width = "1136px";
size.height = "640px";
} else if (dimension == "full_768x1024") {
size.width = "768px";
size.height = "1024px";
} else if (dimension == "full_1024x768") {
size.width = "1024px";
size.height = "768px";
} else if (dimension == "full_800x1280") {
size.width = "800px";
size.height = "1280px";
} else if (dimension == "small") {
size.width = "120px";
size.height = "20px";
} else if (dimension == "large") {
size.width = "216px";
size.height = "36px";
} else if (dimension == "large") {
size.width = "216px";
size.height = "36px";
} else if (dimension == "medium") {
size.width = "168px";
size.height = "68px";
}
return size;
}
function sizeForDimensionSwitch(dimension) {
var size = {};
switch(dimension) {
case "mma":
size.width = "300px";
size.height = "50px";
break;
case "medrect":
size.width = "300px";
size.height = "250px";
break;
case "xxlarge":
size.width = "320px";
size.height = "50px";
break;
case "xlarge":
size.width = "300px";
size.height = "50px";
break;
case "sky":
size.width = "120px";
size.height = "600px";
break;
case "widesky":
size.width = "160px";
size.height = "600px";
break;
case "leader":
size.width = "728px";
size.height = "90px";
break;
case "full_320x480":
size.width = "320px";
size.height = "480px";
break;
case "full_480x320":
size.width = "480px";
size.height = "320px";
break;
case "full_640x960":
size.width = "640px";
size.height = "960px";
break;
case "full_960x640":
size.width = "960px";
size.height = "640px";
break;
case "full_1136x640":
size.width = "1136px";
size.height = "640px";
break;
case "full_768x1024":
size.width = "768px";
size.height = "1024px";
break;
case "full_1024x768":
size.width = "1024px";
size.height = "768px";
break;
case "full_800x1280":
size.width = "800px";
size.height = "1280px";
break;
case "small":
size.width = "120px";
size.height = "20px";
break;
case "large":
size.width = "216px";
size.height = "36px";
break;
case "large":
size.width = "216px";
size.height = "36px";
break;
case "medium":
size.width = "168px";
size.height = "68px";
break;
}
return size;
}
// Best Case Scenario
var date = new Date();
for(var i= 0; i < 1000000; i++) {
dimensionSize['mma'];
}
console.log('Hash BCS',new Date().getTime() - date.getTime())
var date1 = new Date();
for(var i = 0; i < 1000000; i++) {
sizeForDimension('mma');
}
console.log('IF BCS', new Date().getTime() - date1.getTime())
for(var i = 0; i < 1000000; i++) {
sizeForDimensionSwitch('mma');
}
console.log('Switch BCS', new Date().getTime() - date1.getTime())
// Worst Case Scenario
var date = new Date();
for(var i= 0; i < 1000000; i++) {
dimensionSize['medium'];
}
console.log('Hash WCS', new Date().getTime() - date.getTime())
var date1 = new Date();
for(var i = 0; i < 1000000; i++) {
sizeForDimension('medium');
}
console.log('IF WCS', new Date().getTime() - date1.getTime())
for(var i = 0; i < 1000000; i++) {
sizeForDimensionSwitch('medium');
}
console.log('Switch WCS', new Date().getTime() - date1.getTime())
// Chrome
Best Case Scenario (lower is better)
Hash 18ms
IF 40ms
Switch 78ms
Worst Case Scenario (lower is better)
Hash 5ms
IF 29ms
Switch 55ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment