Skip to content

Instantly share code, notes, and snippets.

@getify
Last active July 23, 2022 19:55
Show Gist options
  • Save getify/d0cdddfa4673657a9941 to your computer and use it in GitHub Desktop.
Save getify/d0cdddfa4673657a9941 to your computer and use it in GitHub Desktop.
class vs OLOO
class Foo {
constructor(x,y,z) {
Object.assign(this,{ x, y, z });
}
hello() {
console.log(this.x + this.y + this.z);
}
}
var instances = [];
for (var i=0; i<500; i++) {
instances.push(
new Foo(i,i*2,i*3)
);
}
instances[37].hello(); // 222
var Foo = {
hello() {
console.log(this.x + this.y + this.z);
}
};
function OD(prot,obj) { return Object.assign(Object.create(prot),obj); }
var instances = [];
for (var i=0; i<500; i++) {
instances.push(
OD(Foo,{ x: i, y: i*2, z: i*3 })
);
}
instances[37].hello(); // 222
function Foo(x,y,z) {
return {
hello() {
console.log(this.x + this.y + this.z);
},
x,
y,
z
};
}
var instances = [];
for (var i=0; i<500; i++) {
instances.push(
Foo(i,i*2,i*3)
);
}
instances[37].hello(); // 222
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment