classes vs objects

Benchmark created on


Description

create many objects vs creating many class instances

Setup

class Apa {
    #_name;
    #_age;
    #_id;
    constructor(name, age) {
        this.#_name = name;
        this.#_age = age;
        this.#_id = `Apa/${name}/${age}`;
    }
    get name() {
        return this.#_name;
    }
    get age() {
        return this.#_age;
    }
    get id() {
        return this.#_id;
    }
}

class Apa2 {
    _name;
    _age;
    _id;
    constructor(name, age) {
        this._name = name;
        this._age = age;
        this._id = `Apa/${name}/${age}`;
    }
    get name() {
        return this._name;
    }
    get age() {
        return this._age;
    }
    get id() {
        return this._id;
    }
}

function apa(name, age) {
    return {
        name,
        age,
        id: `Apa/${name}/${age}`,
    };
}

const itemsCount = {
    "10k": 10e+3,
	"100k": 100e+3,
	 "1M": 1e+6,
	 "10M": 10e+6,
};

function loop(times, fn) {
	for (let idx=0; idx<times; ++idx) {
		fn();
	}
}

Test runner

Ready to run.

Testing in
TestOps/sec
10k instances

loop(
	itemsCount["10k"],
	() => new Apa("Ola", 12)
);
ready
10k objects
loop(
	itemsCount["10k"],
	() => apa("Ola", 12)
);
ready
10k instances (public props)
loop(
	itemsCount["10k"],
	() => new Apa2("Ola", 12)
);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.