Prototype vs Constructor
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
function CtorAnimal(name) {
this.name = name;
}
CtorAnimal.prototype.getName = function() {
return this.name;
};
function CtorDog(name) {
CtorAnimal.call(this, name);
}
CtorDog.prototype = Object.create(CtorAnimal.prototype);
CtorDog.prototype.speak = function() {
return "woof";
};
ProtoAnimal = Object.create(Object);
ProtoAnimal.name = "";
ProtoAnimal.getName = function() {
return this.name;
};
ProtoDog = Object.create(ProtoAnimal);
ProtoDog.speak = function() {
return "woof";
};
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test | Ops/sec | |
---|---|---|
Prototype Style
|
|
pending… |
Constructor
|
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit
to the URL.
- Revision 1: published
- Revision 2: published
- Revision 3: published
- Revision 4: published Class creation options
- Revision 5: published Class creation options
- Revision 6: published
0 Comments