Compatibility vs. Performance

Compatibility vs. Performance

  • Comments 9

Earlier I mentioned that two of the design goals for JScript .NET were high performance and compatibility with JScript Classic. Unfortunately these are somewhat contradictory goals! JScript Classic has many dynamic features which make generation of efficient code difficult. Many of these features are rarely used in real-world programs. Others are programming idioms which make programs hard to follow, difficult to debug and slow.

 

JScript .NET therefore has two modes: compatibility mode and fast mode. In compatibility mode there should be almost no JScript program which is not a legal JScript .NET program. Fast mode restricts the use of certain seldom-used features and thereby produces faster programs.

 

The JSC.EXE command-line compiler and ASP.NET both use fast mode by default. To turn fast mode off in JSC use the /fast- switch.

 

Fast mode puts the following restrictions on JScript .NET programs:

 

* All variables must be declared with the var keyword. As I discussed earlier, in JScript Classic it is sometimes legal to use a variable without declaring it. In those situations, the JScript Classic engine automatically creates a new global variable but when in fast mode, JScript .NET does not. This is a good thing -- not only is the code faster but the compiler can now catch spelling errors in variable names.

* Functions may not be redefined. In JScript Classic it is legal to have two or more identical function definitions which do different things. Only the last definition is actually used. This is not legal in JScript .NET in fast mode. This is also goodness, as it eliminates a source of confusion and bugs.

* Built-in objects are entirely read-only. In JScript Classic it is legal to add, modify and (if you are perverse), delete some properties on the Math object, the String prototype and the other built-in objects.

* Attempting to write to read-only properties now produces errors. In JScript Classic writing to a read-only property fails silently, in keeping with the design principle I discussed earlier: muddle on through.

* Functions no longer have an arguments property. The primary use of the arguments property is to create functions which take a variable number of arguments. JScript .NET has a specific syntax for creating such a function. This makes the arguments object unnecessary. To create a JScript .NET function which takes any number of arguments the syntax is:

function MyFunction(... args : Object[] )

{

  // now use args.length, args[0], etc.

}

 

Generally speaking, unclear code is slow code. If the compiler is unable to generate good code it is usually because the restrictions on the objects described in the code are so loose as to make optimization impossible. These few restrictions not only let JScript .NET generate faster code, they also enforce good programming style without overly damaging the "scripty" nature of the language. And if you must run code which has undeclared variables, redefined functions, modified built-in objects or reflection on the function arguments, then there is always compatibility mode to fall back upon.

 

JScript .NET also provides warnings when programming idioms could potentially produce slow code. For example, recall my earlier article on string concatenation.  Using the += operator on strings now produces a warning which suggests using a StringBuilder instead. JScript .NET also produces warnings when code is likely to be incorrect. For example, using a variable before initializing it produces a warning.  So does branching out of a finally block now produce warnings, and so on.

  • * Built-in objects are entirely read-only. In JScript Classic it is legal to add, modify and (if you are perverse), delete some properties on the Math object, the String prototype and the other built-in objects. Glad you didn't call this "goodness". My host adds a few dozen string functions for ease of use (justify, space, repeat). I see being able to expand the built-in library as a strength of ECMAScript. It also overwrites Function.prototype.toString ;-) Sure would be nice if it were possible to enable a "mode" to add to these built-in objects, yet still get the benefits of complation.
  • Disclaimer: I'm very much of a JavaScript/ECMAScript aficionado. If you want to see the extent, check out http://w3future.com/html/beyondJS/ to see the BeyondJS library developed by Sjoerd Visscher and myself. When I first heard of JScript.NET I was ecstatic - here was the chance for JavaScript to make it into the big-leagues. Becoming a full-blown member of the .NET environment would allow JavaScript to go beyond browser scripting into the field of real, main-stream application development. After playing a bit with JScript.NET I have to admit I became disappointed. It seems to me that in order to play in the .NET field and provide an acceptable level of performance, JScript.NET abandons many of the features that make JavaScript such a cool and unique language. Instead it becomes something of a C# clone. Put another way: given that C# is the main language of .NET, I can't see any major reasons to pick JScript.NET over C# (and apparently I'm not the only one: http://blogs.gotdotnet.com/EricLi/commentview.aspx/cd88c241-27f4-410c-b3a4-716cae1b63db why did they choose C# and not JScript.NET?) With regards to Eric's points specifically: 1. I wholeheartedly approve of enforced var use. The only addition I would ask for is type inference not only for performance but also for verifying correctness, ML-style. 2. This is what makes functional programming in JavaScript possible. I'm guessing you also got rid of unnamed functions (lambdas) and closures. 3. The JavaScript model of object as directory is one of the cool things about it that makes it so unique. And the ability to modify the behavior of builtin objects is also an extremely powerful feature (it is what makes BeyondJS possible). By getting rid of prototypes and adding classes, JScript.NET becomes just another C++/Java/C# clone, instead of a unique and viable choice. 4. A good thing 5. But what about the caller and callee properties? To see more comments I've had about JScript.NET check out: http://www.google.com/custom?sitesearch=lambda.weblogs.com&q=jscript.net+shappir Please note that my intent is not to disparage either .NET or JScript.NET. They are both great technical achievements. My point is simple: the same changes meant to enhance JScript also robed it of its reason for being.
  • Jay: Unfortunately, that breaks pretty much every ASP.NET scenario and makes static analysis very difficult. I'll discuss that more later. Dan: (2) I think you're misunderstanding me. The ability to redefine a function does not make functional programming possible, and no, we did not get rid of closures or anonymous functions in JS.NET. What we got rid of was that this is not legal: function foo() {return 1;} function foo(){return 2;} That's legal in JScript classic -- it just means "discard the lexically earlier function declaration". It's also not legal to redeclare the same variable over and over again in JScript.NET but it is in JScript Classic. The reason why its legal in Classic is because the compilation model is different. Maybe I should blog a bit about that. 3) As I mentioned above, being able to modify built-ins screws up very common ASP scenarios. Also, you seem to think that we've removed prototype inheritance. We didn't remove hardly anything, we just added a whole lot of stuff. 5) The caller property was broken already -- ever tried it with recursion on the stack? It doesn't work because it was badly designed from the start. A caller isn't a function, its an activation context, but activation contexts are not first class. I'll expand on these points next week.
  • Let me try to clarify my point. .NET was design as a multi-language platform, that is one of its stated advantages over Java and the JVM. At the same time, C# is the language of choice for this platform. Thus any other language created or ported to .NET must somehow justify it existence by providing an incentive to select it over C#. I can think of several reasons for a programming language to become a viable alternative to C# : 1. A legacy programming language - easier migration and code porting. 2. Different target audience, e.g. power sacrificed for ease of use. 3. Represents a different programming paradigm, e.g. functional, logic, goal-oriented. 4. Targets a different set of problems or problem domains (a DSL perhaps). On the face of it JScript.NET falls in to the first category, yet I don't think this is the case. JavaScript has not been commonly used AFAIK for application development. The vast majority of JavaScript "programs" out there are web pages, and this is simply not .NET. Some ASP was done in JScript, but I think the changes required to port to ASP.NET, and take real advantage of that platform, are such that code reuse is not such an issue. Even if this category is valid for JScript.NET, it is the worst of the 4 to be in. The old code will be ported, but from that point on all new code will be done in C#. After all, .NET makes it easy for different programming languages to talk to each other. As for target audience - I don't think JScript.NET makes it that much easier to develop than C#. I believe that very quickly the developer will find herself writing code that is directly translatable to C# - classes, interfaces, the lot. I also don't see JScript.NET as being targeted at a substantially different set of problems than C#. It is certainly not a DSL. So what's left: the different paradigm. And herein lies my complaint. JavaScript does represent a significantly different programming paradigm than most other OO languages such as C++, Java and C#: everything is an object, objects are extensible, duck-typing all the way. JScript.NET, particularly in fast mode, lets most of this go. Thus no different paradigm - no reason for being. I may be off on all of this, and if so I’d be very happy. I am, as I professed, as JavaScript aficionado, and I would like to see this language prosper. I'm glad to hear that closures and anonymous functions are still in. Closures are what I miss most when working in C++ or Java, and just having them might make all the difference. So, in fast mode, am I allowed to write: var foo = function() { return 1; }; foo = function() { return 2; }; Also, can I use a function reference (object) as a delegate? And if so, is the closure preserved? Having closures seems to mean that you are not using stack frames in the same way as C# or VB.NET, is this the case?
  • > So, in fast mode, am I allowed to write: Absolutely, yes. > Also, can I use a function reference (object) as a delegate? JScript .NET can coerce a function reference to an existing CLR delegate type, but provides no syntax for declaring new CLR delegate types. Unfortunately we did not have time to get that feature in. The rest of your points I'll follow up on privately.
  • Eric: I'd be interested in hearing about the rest of your points too. One of the reasons I like JavaScript is its "prototype" programming paradigm based on some ideas pioneered in the "Self" language/system. IMHO, it appears that JScript.NET is moving (has moved) away from those roots. Personally, I would have enjoyed seeing more "Self-isms"... perhaps allowing one to provide runtime prototyping in a live system, rather than a move to static compilation and static type checking. On the other hand, this would likely be at odds with generating efficient .NET code... the JIT compiler for Self was/is a lot more sophisticated than the one (I am sure) the .NET VM has.
  • This is somewhat off the point of this thread but generally related. I do considerable programming in Javascript/Jscript in the form of DHTML applications using .HTAs. I won't go into detail about why but having application like functionality while maintaining script compatibility with IE is one good reason. In other forums (www.dotnet247.com) someone was talking about the possibility (likelihood?) that Jscript.NET might be made clientside. I refer to this message [Hi, There are no concrete plans for this at the moment but that could change. We are looking for customer feedback on this (there is very little so far).
    Embedding JScript .NET inside the browser is definitely a "cool" thing to do, but unless customers come back and say "Hey, this would really make my life easy!" or "This enables a whole bunch of new scenarios for me" it's
    probably not going to happen. -- Peter Torr ]
    Peter speaks as if he works for Microsoft. My question... Might you embed Jscript.NET in IE7? If you need scenarios as to why this would be worthwhile I can supply a few concrete ones. In general I'd like to stimulate conversation on this topic, perhaps on its own thread.

    Thomas Giebink
  • Peter was the program manager for JScript .NET.

    I would personally love to see JS.NET in IE7 but I know of no plans to make it happen. Feel free to post any ideas you might have and I'll pass them on to the people who presently own script.
  • PingBack from http://performance.survol.fr/2008/05/un-mode-fast/

Page 1 of 1 (9 items)