new vs create
JavaScript performance comparison
Preparation code
<script>
var Ctor = function() {};
Ctor.prototype.variable = 'test';
Ctor.prototype.method = function() {
return this.variable;
};
var Create = {
variable: 'test',
method: function() {
return this.variable;
}
};
var Literal = function() {
return {
variable: 'test',
method: function() {
return this.variable;
}
};
};
(function() {
var variable = 'test';
window.Factory = function() {
return {
variable: variable,
method: function() {
return this.variable;
}
};
};
}());
var HomebrewCreate = function(p) {
function f() {}
f.prototype = p;
return new f();
}
var Ctor2 = function() {};
Ctor2.prototype = {
variable: 'test',
method: function() {
return this.variable;
}
};
var Ctor3 = function() {};
Ctor3.prototype = Ctor.prototype;
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
new |
|
pending… |
create |
|
pending… |
factory |
|
pending… |
literal |
|
pending… |
homebrew create |
|
pending… |
Ctor with re-set .prototype |
|
pending… |
Ctor with re-linked prototype |
|
pending… |
Homebrew create based on existing prototype |
|
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. Here’s a list of current revisions for this page:
- Revision 4: published by herby
0 comments