Skip to content

Instantly share code, notes, and snippets.

@robbiespeed
Created November 17, 2024 02:49
Show Gist options
  • Save robbiespeed/ed4c49075263fd6a4ede259f35aaf6e2 to your computer and use it in GitHub Desktop.
Save robbiespeed/ed4c49075263fd6a4ede259f35aaf6e2 to your computer and use it in GitHub Desktop.
JS Class Perf
import { bench, run } from 'mitata';
const rCount = 1000;
const iCount = 1000;
class FooN {
#fooState = 1;
#inc = 13;
run() {
return (this.#fooState += this.#inc);
}
}
class BarN extends FooN {
#barState = 1;
run() {
return (this.#barState += super.run());
}
}
class FooC {
_fooState = 1;
_inc = 13;
run() {
return (this._fooState += this._inc);
}
}
class BarC extends FooC {
_barState = 1;
run() {
return (this._barState += super.run());
}
}
function FooF() {
this._fooState = 1;
this._inc = 13;
}
FooF.prototype.run = function run() {
return (this._fooState += this._inc);
};
function BarF() {
FooF.call(this);
this._barState = 1;
}
Object.setPrototypeOf(BarF.prototype, FooF.prototype);
Object.setPrototypeOf(BarF, FooF);
const fooFProto = FooF.prototype;
BarF.prototype.run = function run() {
return (this._barState += fooFProto.run.call(this));
};
function barRunFlat() {
return (this._barState += this._fooState += this._inc);
}
function createBarFlat() {
return {
_fooState: 1,
_inc: 13,
_barState: 1,
run: barRunFlat,
};
}
function createBarFlatEncapsulated() {
let fooState = 1;
const inc = 13;
let barState = 1;
return {
run() {
return (barState += fooState += inc);
},
};
}
function fooRunComposed() {
return (this._fooState += this._inc);
}
function createFooComposed() {
return {
_fooState: 1,
_inc: 13,
run: fooRunComposed,
};
}
function barRunComposed() {
return (this._barState += this._foo.run());
}
function createBarComposed() {
return {
_barState: 1,
_foo: createFooComposed(),
run: barRunComposed,
};
}
function createFooComposedEncapsulated() {
let fooState = 1;
const inc = 13;
return {
run() {
return (fooState += inc);
},
};
}
function createBarComposedEncapsulated() {
const foo = createFooComposedEncapsulated();
let barState = 1;
return {
run() {
return (barState += foo.run());
},
};
}
bench('Object Literal (Flat)', () => {
let i = iCount;
while (i-- > 0) {
let r = rCount;
const bar = createBarFlat();
while (r-- > 0) {
bar.run();
}
}
});
bench('Object Literal (Flat Encapsulated)', () => {
let i = iCount;
while (i-- > 0) {
let r = rCount;
const bar = createBarFlatEncapsulated();
while (r-- > 0) {
bar.run();
}
}
});
bench('Object Literal (Composed)', () => {
let i = iCount;
while (i-- > 0) {
let r = rCount;
const bar = createBarComposed();
while (r-- > 0) {
bar.run();
}
}
});
bench('Object Literal (Composed Encapsulated)', () => {
let i = iCount;
while (i-- > 0) {
let r = rCount;
const bar = createBarComposedEncapsulated();
while (r-- > 0) {
bar.run();
}
}
});
bench('FunctionConstructor', () => {
let i = iCount;
while (i-- > 0) {
let r = rCount;
const bar = new BarF();
while (r-- > 0) {
bar.run();
}
}
});
bench('Class', () => {
let i = iCount;
while (i-- > 0) {
let r = rCount;
const bar = new BarC();
while (r-- > 0) {
bar.run();
}
}
});
bench('ClassPrivates', () => {
let i = iCount;
while (i-- > 0) {
let r = rCount;
const bar = new BarN();
while (r-- > 0) {
bar.run();
}
}
});
await run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment