8

So, when we have a simple constructor:

function Vec(x, y) {
    this.x = x;
    this.y = y;
}

And a factory analogue:

function VecFactory(x, y) {
    return {
        x: x,
        y: y
    }
}

The performance is comparable:

100000000 constructors: 1874 ms
100000000 factory calls: 1782 ms

But when we add a prototype:

function VecFactory(x, y) {
    let result = {
        x: x,
        y: y
    }
    Object.setPrototypeOf(result, Vec.prototype);
    return result;
}

The performance drops drastically

100000000 factory calls: 13251 ms

Assigning to __proto__ makes it worse, cloning all prototype methods into every object even worse still.

So, why does this happen? Is there a way to improve factory performance?

UPD: using Object.create instead of Object.setPrototypeOf, as Bergi suggests, drops factory call time to around 8700 ms.

lvl5hm
  • 81
  • 2
  • 2
    You should use `Object.create` over `Object.setPrototypeOf` – Bergi Feb 04 '17 at 19:53
  • Which browser/environment (version) are you testing in? Different engines have different optimisations in place for these things. – Bergi Feb 04 '17 at 19:54
  • I'm using latest chrome/v8. Using `Object.create` does reduce time to about 8700ms, thank you) – lvl5hm Feb 04 '17 at 20:00
  • 2
    Here is an answer to this question: http://stackoverflow.com/questions/23807805/why-is-mutating-the-prototype-of-an-object-bad-for-performance – jetpackpony Feb 04 '17 at 20:19
  • So, messing with prototypes at object instantiation is always bad for performance. Makes sense, I guess. Sucks for factories though – lvl5hm Feb 04 '17 at 20:30
  • They *should* be able to optimise both `Object.create` and `Object.setPrototypeOf({})` to the same performance as the constructor, but classes get special love from v8's optimiser because they are so popular. Needs more support from the community :-) – Bergi Feb 04 '17 at 22:35
  • 1
    @lvl5hm Why don't you use a constructor function inside the factory? It will be one function call slower than a constructor.. – pishpish Feb 13 '17 at 21:13

0 Answers0