0

As you know Cappuccino implements the dispatch mechanism of Objective-C / Smalltalk to send messages to objects (~call their methods) in a special method called objj_msgSend.

[someObject someMethodToInvocate: aParameter];

Obviously this introduces some overhead and therefor speed-loss. I'd like to know if somebody can provide a speed comparison between this Message Sending and the normal way to execute a method in JavaScript…

someObject.someMethodToInvocate(aParameter);
Richard Durr
  • 3,081
  • 3
  • 20
  • 26
  • 2
    Maybe there is a difference, but the question is: does it matter? There is probably more differences between the different javascript engines in the browsers. Do some profiling to see where your code spend most of it's time, and see if you can optimize that. "Premature optimization..." – some Nov 11 '10 at 16:50
  • This is a general question, so yes, it does matter. This is not premature optimization either, because I'd like to evaluate feasibility, not optimize some code. Especially since obj_msgSend gets called that very very very often. – Richard Durr Nov 11 '10 at 17:40

3 Answers3

4

In your comments you say you're wondering 'in general' in the context of Cappuccino applications. In that case the test is easy: run any Cappuccino application, such as GitHub Issues, and judge for yourself if its slow or not. Try scrolling in the main table, select a few entries and so on. That'll tell you if Cappuccino is fast or slow 'in general' as objj_msgSend is used extensively in any use case you can think of in an application like this.

If you're actually thinking of something more specific after all, note that nothing about Cappuccino forces you to use message passing. Just like in Objective-C you can always 'drop down to the metal' - pure JavaScript in this case - when you need to do something more performance intensive. If you have a tight loop, and you don't require the additional functionality provided by objj_msgSend, simply call functions directly. Objective-J won't mind.

Alexander Ljungberg
  • 6,302
  • 4
  • 31
  • 37
  • Actually I said "in general" and not in the context of Cappuccino applications. I did try out most of the avaiable Cappuccino examples, but as you said yourself: you can always drop down to the metal. Therefor the speed-impact of objj_msgSend is not really assessable. Since nobody seems to have an answer ready for that, I'll have to make some benchmarks myself. Thanks nevertheless. – Richard Durr Nov 12 '10 at 08:36
1

objj_msgSend is for my simple tests of pure method calling about 2–2.5 times slower than a direct call.

That is actually quite good, given the advanced features it makes possible.

Richard Durr
  • 3,081
  • 3
  • 20
  • 26
-2

This is coming two years too late, but this is a slightly invalid question (in no way saying that makes it a bad question). There is really no point questioning the speed of objj_msgSend, not when you are assuming that it is a Smalltalk/Obj-C/Obj-J specific feature.

Javascript has ALWAYS had this ability.

Lookup: the call() AND apply() methods... (a quick google search will bring up articles like this -> http://vikasrao.wordpress.com/2011/06/09/javascripts-call-and-apply-methods/ )

It is the same issue with jQuery/Prototype/etc..., they are all fine and dandy and useful. But they hurt the development community because everyone relies on these frameworks instead of learning the core language features that make any language useful.

Do yourself and the development community a favor and LEARN YOUR LANGUAGES, NOT FRAMEWORKS. If you know the languages you use, the frameworks you use are irrelevant, use them or just build them yourself, because at that point you should be able to.

Hope that came off as helpful and not condescending, thats not my intention. :)

G. Shearer
  • 2,175
  • 17
  • 19
  • ...now that i feel a little dumb, looking at your title it does say 'vs call'. So you are aware. My point is still valid though, if you are concerned about the speed of that method, why not just use the built in lanaguage feature... :) – G. Shearer Mar 03 '12 at 17:30
  • You're not going to build. Cappuccino class application without a framework like cappuccino. The frameworks DOES matter. – Me1000 Mar 03 '12 at 22:15
  • yay downvotes (sarcasm). I'm just going to ignore that my answer is not all that different than the highest voted one... Actually, it is not difficult to emulate Cocoa without a framework like cappuccino. The majority of the most useful things (AppKit/UIKit) can be emulated with a few hundred lines of well written code. I know, I've done it. The bigger point is you are building web content, it is not the same as the 'normal' application domain. Capp is sweet, but it caters to a different world than the web. Yay its MVC, but you lose HTML/CSS, the most useful parts of a browser. – G. Shearer Jun 20 '12 at 02:02
  • *you are not going to build a (Good) Cappuccino Class app without understanding why you use certain patterns in one domain and certain domains in another* -- Look at most Java programmers code, when all you make are iHammerFactory's all the time everything starts looking like an iNail, but use that pattern in other domains and you start running into trouble – G. Shearer Jun 20 '12 at 02:06