Skip to content

Instantly share code, notes, and snippets.

@jstsch
Created April 8, 2012 23:33
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 jstsch/2340375 to your computer and use it in GitHub Desktop.
Save jstsch/2340375 to your computer and use it in GitHub Desktop.
JS OOP comparision
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>untitled</title>
<script type="text/javascript" charset="utf-8">
/* <![CDATA[ */
var startTime = new Date().getTime();
Cat = {
createNew: function(){
var cat = { };
cat.makeSound = function(){ //add logic
var j = 0;
for (var i = 0; i < 100; i++) {
j += Math.round(Math.random() * 100);
}
return j;
}
return cat;
}
}
var j = 0;
for (var i = 0; i < 10000; i++) {
var myCat = Cat.createNew();
j += myCat.makeSound();
}
var endTime = new Date().getTime();
console.log(j);
console.log(endTime - startTime);
/* ]]> */
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>untitled</title>
<script type="text/javascript" charset="utf-8">
/* <![CDATA[ */
var startTime = new Date().getTime();
function Cat() {
}
Cat.prototype.makeSound = function() {
var j = 0;
for (var i = 0; i < 100; i++) {
j += Math.round(Math.random() * 100);
}
return j;
};
var j = 0;
for (var i = 0; i < 10000; i++) {
var myCat = new Cat();
j += myCat.makeSound();
}
var endTime = new Date().getTime();
console.log(j);
console.log(endTime - startTime);
/* ]]> */
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment