Skip to content

Instantly share code, notes, and snippets.

@kmcminn
Created May 20, 2013 06:37
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 kmcminn/5610731 to your computer and use it in GitHub Desktop.
Save kmcminn/5610731 to your computer and use it in GitHub Desktop.
jsperf
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script src="benchmark.js"></script>
<div id="testresult1">
</div>
<div id="testresult2">
</div>
<div id="testresult3">
</div>
<div id="testresult4">
</div>
<script>
// basic timer shit
var Timerbot = function(n) {
this.name = n;
this.start = Date.now();
this.end = Date.now();
this.deltas = [];
this.reset = function() {
this.deltas = null;
this.deltas = [];
this.start = Date.now();
this.end = 0;
};
this.recordStart = function() {
this.start = Date.now();
};
this.recordEnd = function() {
this.end = Date.now();
this.deltas.push(this.end - this.start);
};
this.topFive = function() {
var tmp = this.deltas.sort(function(a, b) {
return a > b;
});
return [ tmp[tmp.length - 1], tmp[tmp.length - 2], tmp[tmp.length - 3], tmp[tmp.length - 3], tmp[tmp.length - 5] ];
};
this.summaryText = function(arr) {
var arr = arr || this.deltas;
var tmp = arr.sort(function(a, b) {
return a > b;
});
return this.name + ": highest 5 deltas was " + tmp[tmp.length - 1] + ", " + tmp[tmp.length - 2] + ", " + tmp[tmp.length - 3] + ", " + tmp[tmp.length - 4] + ", " + tmp[tmp.length - 5] + " ms and the median was " + tmp[Math.floor(tmp.length / 2)] + " ms";
}
};
// test #1
var TestTimer1 = TestTimer1 || new Timerbot("TestClass1");
var TestClass1 = new(function() {
this.a = 0;
this.b = 0;
this.c = 0;
this.d = 0;
this.m1 = function() {
this.a += 1;
this.b += 2;
this.c += 3;
this.d += this.a;
};
this.m2 = function() {
this.a += 4;
this.b += 5;
this.c += 6;
this.d += this.b;
};
this.m3 = function() {
this.a += 7;
this.b += 8;
this.c += 9;
this.d += this.c;
};
this.m = function() {
this.m1();
this.m2();
this.m3();
};
})();
// test #2
var TestTimer2 = TestTimer2 || new Timerbot("TestClass2");
function Prototype2() {}
Prototype2.prototype.a = 0;
Prototype2.prototype.b = 0;
Prototype2.prototype.c = 0;
Prototype2.prototype.d = 0;
Prototype2.prototype.m1 = function() {
this.a += 1;
this.b += 2;
this.c += 3;
this.d += this.a;
};
Prototype2.prototype.m2 = function() {
this.a += 4;
this.b += 5;
this.c += 6;
this.d += this.b;
};
Prototype2.prototype.m3 = function() {
this.a += 7;
this.b += 8;
this.c += 9;
this.d += this.c;
};
Prototype2.prototype.m = function() {
this.m1();
this.m2();
this.m3();
};
var TestClass2 = new Prototype2();
// test #3
var TestTimer3 = TestTimer3 || new Timerbot("TestClass3");
var TestClass3 = new((function() {
var a = 0;
var b = 0;
var c = 0;
var d = 0;
var m1 = function() {
a += 1;
b += 2;
c += 3;
d += a;
};
var m2 = function() {
a += 4;
b += 5;
c += 6;
d += b;
};
var m3 = function() {
a += 7;
b += 8;
c += 9;
d += c;
};
var m = function() {
m1();
m2();
m3();
};
return function() {
this.m = m
};
})())();
// test #4
var TestTimer4 = TestTimer4 || new Timerbot("TestClass4");
var TestClass4 = (function() {
var a = 0;
var b = 0;
var c = 0;
var d = 0;
var m1 = function() {
a += 1;
b += 2;
c += 3;
d += a;
};
var m2 = function() {
a += 4;
b += 5;
c += 6;
d += b;
};
var m3 = function() {
a += 7;
b += 8;
c += 9;
d += c;
};
var m = function() {
m1();
m2();
m3();
};
return {
m: m
}
})();
var benchmark = function() {
var suite = new Benchmark.Suite;
suite.add("simple class using this+new constructor", function(){
for (var i = 0; i < 1000; i += 1) {
TestTimer1.recordStart();
TestClass1.m();
TestClass1.m();
TestClass1.m();
TestTimer1.recordEnd();
}
})
.add("prototypal class using this", function(){
for (var i = 0; i < 1000; i += 1) {
TestTimer2.recordStart();
TestClass2.m();
TestClass2.m();
TestClass2.m();
TestTimer2.recordEnd();
}
})
.add("function closured class", function(){
for (var i = 0; i < 1000; i += 1) {
TestTimer3.recordStart();
TestClass3.m();
TestClass3.m();
TestClass3.m();
TestTimer3.recordEnd();
}
})
.add("object closured class", function(){
for (var i = 0; i < 1000; i += 1) {
TestTimer4.recordStart();
TestClass4.m();
TestClass4.m();
TestClass4.m();
TestTimer4.recordEnd();
}
})
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
}).run({async: true});
};
var elem1 = document.getElementById("testresult1");
var elem2 = document.getElementById("testresult2");
var elem3 = document.getElementById("testresult3");
var elem4 = document.getElementById("testresult4");
function displayResults(){
if (TestTimer1.deltas.length > 0) {
var t1 = t1 || [];
t1 = t1.concat(TestTimer1.topFive())
elem1.innerHTML = TestTimer1.summaryText(t1) + "<br>";
}
if (TestTimer2.deltas.length > 0) {
var t2 = t2 || [];
t2 = t2.concat(TestTimer2.topFive())
elem2.innerHTML = TestTimer2.summaryText(t2) + "<br>";
}
if (TestTimer3.deltas.length > 0) {
var t3 = t3 || [];
t3 = t3.concat(TestTimer3.topFive())
elem3.innerHTML = TestTimer3.summaryText(t3) + "<br>";
}
if (TestTimer4.deltas.length > 0) {
var t4 = t4 || [];
t4 = t4.concat(TestTimer4.topFive())
elem4.innerHTML = TestTimer4.summaryText(t4) + "<br>";
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment