Skip to content

Instantly share code, notes, and snippets.

@uupaa
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save uupaa/aa92b45f36c6001c5331 to your computer and use it in GitHub Desktop.

Select an option

Save uupaa/aa92b45f36c6001c5331 to your computer and use it in GitHub Desktop.
Object.create と prototype を適切に使わない Class 定義が如何に遅いか
// function declaration
(function(global) {
"use strict";
function Aaa1(value) {
this._value = value || "";
}
Aaa1.prototype = Object.create(Aaa1, {
constructor: { value: Aaa1 },
value: {
set: function(v) { this._value = v; },
get: function() { return this._value; }
},
concat: { value: Aaa1_concat },
concat$: { value: Aaa1_concat$ },
});
function Aaa1_concat(a) { return this._value + a; }
function Aaa1_concat$(a) { this._value += a; return this; }
global["Aaa1"] = Aaa1;
})((this || 0).self || global);
// bad style
var Aaa2 = function(value) {
"use strict";
this._value = value || "";
Object.defineProperties(this, {
value: {
set: function(v) { this._value = v; },
get: function() { return this._value; }
}
});
this.concat = function(a) {
return this._value + a;
};
this.concat$ = function(a) {
this._value += a;
return this;
};
};
// defineProperties, function declaration
(function(global) {
"use strict";
function Aaa3(value) {
this._value = value || "";
Object.defineProperties(this, {
value: {
set: function(v) { this._value = v; },
get: function() { return this._value; }
}
});
}
Aaa3.prototype.concat = Aaa1_concat;
Aaa3.prototype.concat$ = Aaa1_concat$;
function Aaa1_concat(a) { return this._value + a; }
function Aaa1_concat$(a) { this._value += a; return this; }
global["Aaa3"] = Aaa3;
})((this || 0).self || global);
// defineProperties, function expression
(function(global) {
"use strict";
function Aaa4(value) {
this._value = value || "";
Object.defineProperties(this, {
value: {
set: function(v) { this._value = v; },
get: function() { return this._value; }
}
});
}
Aaa4.prototype.concat = function(a) { return this._value + a; };
Aaa4.prototype.concat$ = function(a) { this._value += a; return this; };
global["Aaa4"] = Aaa4;
})((this || 0).self || global);
// function expression
(function(global) {
"use strict";
function Aaa5(value) {
this._value = value || "";
}
Aaa5.prototype = Object.create(Aaa5, {
constructor: { value: Aaa5 },
value: {
set: function(v) { this._value = v; },
get: function() { return this._value; }
},
concat: { value: function(a) { return this._value + a; } },
concat$: { value: function(a) { this._value += a; return this; } },
});
global["Aaa5"] = Aaa5;
})((this || 0).self || global);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment