L
L
lordos2016-06-16 11:24:40
JavaScript
lordos, 2016-06-16 11:24:40

Inheritance in JS Is it better to use mixins?

There is a fairly large project on node js. Stateful app. At the start, I load data from Mongo and create my own classes based on them. They all inherit from a "main class" that contains most of the methods used and the persistence logic in Mongo. Many classes also inherit from other classes.
Inheritance is implemented like this:

MyClass.prototype = _.extend(Object.create(CommonClass.prototype),SpecialClass.prototype,...);

There is a class that has more than 30 attributes - objects created in this way. Let's call it an object.
It turns out that I, as it were, mix in the necessary functionality into the class prototype.
I have several thousand objects, I start iteration every 100 ms, where for each of them I perform simple actions - I compare attribute values ​​and call prototype methods that change some attribute.
The iteration time for modifying 1,000 items takes approximately 90 ms.
I tried to do the same on view objects
var Ob = function(opts){
  this.attr1 = opts.attr1;
  .....
}

Ob.prototype.attr25 = 500;

Ob.prototype.method1 = function(){
  //расчет расстояния между двумя точками, координаты заложены в атрибутах, некоторые лежат в прототипе (attr25)
  var distance = Math.sqrt(...)
  return distance < 1000;
}

Enumeration of 20,000 such objects takes less time!
Why is that?
As far as I generally correctly approached the implementation?
There is an opportunity to redo everything, but I do not know how better.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Taratin, 2016-06-16
@Taraflex

Go to node 5+
Use es6 classes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question