ded / klass

a utility for creating expressive classes in JavaScript
753 stars 78 forks source link

Extremly slow compared to .inheritWith, .fastClass, ExtJS, TypeScript or Native solutions #16

Closed dotnetwise closed 11 years ago

dotnetwise commented 11 years ago

Please have a look in here https://github.com/dotnetwise/Javascript-FastClass The performance tests are the first links at the top

ded commented 11 years ago

that's great. but these other implementations don't support nearly as many features as klass.

cheers :cake:

dotnetwise commented 11 years ago

That is a fair point, hence I want to be rather constructive and make .fastClass and .inheritWith to cover more features. Can you please let me know what features klass support and you don't find them already included?

Thanks!

ded commented 11 years ago

klass supports two flavors

1) object literal initialization

var MyClass = klass({
  initialize: function () {},
  method1: function () {},
  method2: function () {}
})

2) constructor function + methods

var MyClass = klass(function () {

})
  .methods({
    method1: function () {},
    method2: function () {}
  })

klass supports auto-calling to super classes

var MyClass = klass(function () {
  console.log('called first')
})
var MySubClass = MyClass.extend(function () {
  console.log('called second')
})
var MyLastSubClass = MySubClass.extend(function () {
  console.log('called third')
})

new MyLastSubClass()

klass supports this.supr() in any method

var BaseClass = klass().methods({
  run: function () {
    console.log('run from superclass')
  }
})
var SubClass = BaseClass.extend().methods({
  run: function () {
    console.log('run from subclass')
    this.supr()
  }
})
dotnetwise commented 11 years ago

Thank you for elaborated compharison. Here are my thoughts:

1) object literal initialization

var MyClass = klass({
  initialize: function () {},
  method1: function () {},
  method2: function () {}
})

Interesting, but that can be very easy achievable if we're creating a similar method to .fastClass and add it to Function rather than Function.prtotype. Great Idea! :+1: Usage:

var MyClass = Function.fastClass({
  constructor: function () {},
  method1: function () {},
  method2: function () {}
})

2) constructor function + methods

var MyClass = klass(function () {
})
  .methods({
    method1: function () {},
    method2: function () {}
  })

Looks pretty same as below, right?

var MyClass = function() {
}.define({
 method1: function() {},
 method2: function() {}
});

klass supports auto-calling to super classes

This is something nice to have although it always results in two function calls. So it's a question about coding time vs runtime performance. I bet you consider the first more robust & important whereas could be cases there this is true. But since the inheritance is not for beginner js developers, they will surely remember to call their base constructor, right?

klass supports this.supr() in any method

var BaseClass = klass().methods({
  run: function () {
    console.log('run from superclass')
  }
})
var SubClass = BaseClass.extend().methods({
  run: function () {
    console.log('run from subclass')
    this.supr()
  }
})

Do you realize because of supporting this.supr() you're always, but always actually calling two functions? I think this is the most slowing killer of klass (and all the libraries that have the concept of supr in the first place). Look at the replacement of super below - looks pretty much the same - literraly extra 8 chars for this example.

var BaseClass = function() {}.define({
  run: function () {
    console.log('run from superclass')
  }
});
var SubClass = BaseClass.inheritWith(function(supr) {
 return {
    run: function () {
      console.log('run from subclass')
      supr.run.call(this);
    }
  }
})

So not much of an extra code to write but with big impact in performance tests, right?