Skip to content

Instantly share code, notes, and snippets.

@arikui
Created December 1, 2010 08:49
Show Gist options
  • Save arikui/723178 to your computer and use it in GitHub Desktop.
Save arikui/723178 to your computer and use it in GitHub Desktop.
<!doctype html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=Shift_JIS">
<title>aa</title>
<script type="text/javascript">
var tests = {
case0: testCase("plain", function(i){
var o = {
r: 1,
f: function(n){
this.r += n;
}
};
while(--i) o.f(2);
return o.r;
}),
case1: testCase("closure", function(i){
var o = {
r: 1,
f: function(){
var self = this;
(function(n){
self.r += n;
})(2);
}
};
while(--i) o.f();
return o.r;
}),
case2: testCase("call", function(i){
var o = {
r: 1,
f: function(){
(function(n){
this.r += n;
}).call(this, 2);
}
};
while(--i) o.f();
return o.r;
}),
case3: testCase("apply", function(i){
var o = {
r: 1,
f: function(){
(function(n){
this.r += n;
}).apply(this, [2]);
}
};
while(--i) o.f();
return o.r;
}),
case4: testCase("apply arguments", function(i){
var o = {
r: 1,
f: function(){
(function(n){
this.r += n;
}).apply(this, arguments);
}
};
while(--i) o.f(2);
return o.r;
}),
case5: testCase("apply this value", function(i){
var o = {
r: 1,
a: [2],
f: function(){
(function(n){
this.r += n;
}).apply(this, this.a);
}
};
while(--i) o.f();
return o.r;
}),
case6: testCase("closure 3 nest", function(i){
var o = {
r: 1,
f: function(){
var self = this;
(function(){
(function(){
(function(n){
self.r += n;
})(2);
})();
})();
}
};
while(--i) o.f();
return o.r;
}),
case7: testCase("call 3 nest", function(i){
var o = {
r: 1,
f: function(){
(function(){
(function(){
(function(n){
this.r += n;
}).call(this, 2);
}).call(this);
}).call(this);
}
};
while(--i) o.f();
return o.r;
})
};
function testCase(name, f){
return {
name : name,
count: 0,
sum : 0,
f : f
};
}
</script>
</head><body>
<table>
<tbody><tr>
<th></th>
<th>n</th>
<th>count</th>
<th>total</th>
<th>avr</th>
<th>result</th>
</tr>
</tbody></table>
<script type="text/javascript">
var puts = function(id){
if(tests[id].timer) return;
tests[id].timer = setTimeout(function(){
var test = tests[id];
test.timer = clearTimeout(test.timer);
var n = parseInt(document.getElementById(id + "_n").value, 10);
var s = new Date;
var r = test.f(n);
var elps = new Date - s;
test.sum += elps;
test.count += n;
document.getElementById(id + "_s").innerHTML = test.count;
document.getElementById(id + "_t").innerHTML = elps;
document.getElementById(id + "_a").innerHTML = test.sum / test.count;
document.getElementById(id + "_r").innerHTML = r;
}, 100);
};
// make table
(function(){
for(var x in tests){
var row = createRow(x, tests[x].name);
document.getElementsByTagName("table")[0].appendChild(row);
row.firstChild.firstChild.onclick = function(){
puts(this.id);
};
}
})();
function createRow(id, name){
var tr = document.createElement("tr");
var tds = [
document.createElement("td"),
document.createElement("td"),
document.createElement("td"),
document.createElement("td"),
document.createElement("td"),
document.createElement("td")
];
var button = document.createElement("button");
button.id = id;
button.appendChild(document.createTextNode(name || id));
tds[0].appendChild(button);
var input = document.createElement("input");
input.id = id + "_n";
input.value = "10000";
tds[1].appendChild(input);
tds[2].id = id + "_s";
tds[3].id = id + "_t";
tds[4].id = id + "_a";
tds[5].id = id + "_r";
for(var i = 0, td; td = tds[i++];){
tr.appendChild(td);
}
return tr;
}
</script>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment