Skip to content

Instantly share code, notes, and snippets.

@Talleyran
Created November 15, 2012 20:51
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 Talleyran/4081186 to your computer and use it in GitHub Desktop.
Save Talleyran/4081186 to your computer and use it in GitHub Desktop.
js test
var t,
F,F2,F3,F4,
G,G2,G3,G4,
E,E2,E3,E4,
obj,obj2,obj3,obj4,obj5,obj6,obj7,obj8
F = function(){ this.a = 1 }
F.prototype.setA = function(v){ this.a = v }
F2 = function(){ this.a = 1; this.b = 1 }
F2.prototype = new F
F2.prototype.setB = function(v){ this.b = v }
F3 = function(){ this.a = 1; this.b = 1; this.c = 1 }
F3.prototype = new F2
F2.prototype.setC = function(v){ this.c = v }
F4 = function(){ this.a = 1; this.b = 1; this.c = 1; this.d = 1 }
F4.prototype = new F3
F2.prototype.setD = function(v){ this.d = v }
obj = new F; obj2 = new F2; obj3 = new F3; obj4 = new F4;
obj5 = new F; obj6 = new F2; obj7 = new F3; obj8 = new F4;
t = (new Date).getTime()
for(var i=0; i<100000; i++){
obj.setA(i)
obj5.setA(i)
}
console.log((new Date).getTime() - t + 'ms')
t = (new Date).getTime()
for(var i=0; i<100000; i++){
obj2.setA(i)
obj6.setB(i)
}
console.log((new Date).getTime() - t + 'ms')
t = (new Date).getTime()
for(var i=0; i<100000; i++){
obj3.setA(i)
obj7.setC(i)
}
console.log((new Date).getTime() - t + 'ms')
t = (new Date).getTime()
for(var i=0; i<100000; i++){
obj4.setA(i)
obj8.setD(i)
}
console.log((new Date).getTime() - t + 'ms')
console.log ('-----')
G = function(){ return { a: 1, setA: function(v){ this.a = v } } }
G2 = function(){ var t = G(); t.b = 1; t.setB = function(v){ this.b = v }; return t }
G3 = function(){ var t = G2(); t.c = 1; t.setC = function(v){ this.c = v }; return t }
G4 = function(){ var t = G3(); t.d = 1; t.setD = function(v){ this.d = v }; return t }
obj = G(); obj2 = G2(); obj3 = G3(); obj4 = G4();
obj5 = G(); obj6 = G2(); obj7 = G3(); obj8 = G4();
t = (new Date).getTime()
for(var i=0; i<100000; i++){
obj.setA(i)
obj5.setA(i)
}
console.log((new Date).getTime() - t + 'ms')
t = (new Date).getTime()
for(var i=0; i<100000; i++){
obj2.setA(i)
obj6.setB(i)
}
console.log((new Date).getTime() - t + 'ms')
t = (new Date).getTime()
for(var i=0; i<100000; i++){
obj3.setA(i)
obj7.setC(i)
}
console.log((new Date).getTime() - t + 'ms')
t = (new Date).getTime()
for(var i=0; i<100000; i++){
obj4.setA(i)
obj8.setD(i)
}
console.log((new Date).getTime() - t + 'ms')
console.log ('-----')
var setA = function(v){ this.a = v }
var setB = function(v){ this.b = v }
var setC = function(v){ this.c = v }
var setD = function(v){ this.d = v }
E = function(){ return { a: 1, setA: setA } }
E2 = function(){ var t = E(); t.b = 1; t.setB = setB; return t }
E3 = function(){ var t = E2(); t.c = 1; t.setC = setC; return t }
E4 = function(){ var t = E3(); t.d = 1; t.setD = setD; return t }
obj = E(); obj2 = E2(); obj3 = E3(); obj4 = E4();
obj5 = E(); obj6 = E2(); obj7 = E3(); obj8 = E4();
t = (new Date).getTime()
for(var i=0; i<100000; i++){
obj.setA(i)
obj5.setA(i)
}
console.log((new Date).getTime() - t + 'ms')
t = (new Date).getTime()
for(var i=0; i<100000; i++){
obj2.setA(i)
obj6.setB(i)
}
console.log((new Date).getTime() - t + 'ms')
t = (new Date).getTime()
for(var i=0; i<100000; i++){
obj3.setA(i)
obj7.setC(i)
}
console.log((new Date).getTime() - t + 'ms')
t = (new Date).getTime()
for(var i=0; i<100000; i++){
obj4.setA(i)
obj8.setD(i)
}
console.log((new Date).getTime() - t + 'ms')
console.log ('-----')
t = (new Date).getTime()
for(var i=0; i<100000; i++){
new F4
}
console.log((new Date).getTime() - t + 'ms')
t = (new Date).getTime()
for(var i=0; i<100000; i++){
G4()
}
console.log((new Date).getTime() - t + 'ms')
t = (new Date).getTime()
for(var i=0; i<100000; i++){
E4()
}
console.log((new Date).getTime() - t + 'ms')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment