If this page does not print out automatically, select Print from the File menu.

Hands on: Javascript comes of age

Adobe’s muscle is taking Javascript beyond simple web scripts

Tim Anderson, Personal Computer World 28 Mar 2007

In 1995 a programmer called Brendan Eich joined Netscape Communications.

Netscape “was looking for someone to work on a scripting language or some kind of language inside the browser that could be used to automate parts of a web page or make a web page more dynamic”, wrote Eich in an article on Netscape’s website.

The language was initially called Mocha, then Livescript, and finally Javascript, in a deliberate attempt to ride the waves of Java’s success.

The name still causes confusion, since Javascript is completely different from Java. Microsoft attempted to derail it with VBscript, but this was not implemented by browsers other than Internet Explorer, making Javascript the only choice for scripting web pages.

Javascript was standardised by ECMA, a European standards body, and the official specification is called ECMAscript. Microsoft’s implementation is called Jscript, while Adobe has a version running in Flash called Actionscript.

Javascript is widely used, but not really loved. There are many compatibility issues, mainly with the document object model presented by different browsers. Javascript developers have to learn tricks to get code working everywhere. These issues, combined with the perception that Javascript is only for scripting, meant that few developers attempted to use Javascript for substantial applications.

This is changing and Javascript usage is growing fast. The first key factor is the rise of Ajax, where web applications have extensive client-side script to give users a richer, more seamless user interface.

The second factor is the use of Flash for applications beyond multimedia effects. Adobe’s Flex product, soon to be joined by a related project called Apollo, lets you code for Flash with XML and Actionscript, which is easier for developers than the designer-oriented Flash IDE.

Object orientation
Javascript has long supported object oriented programming, although if you only use it for scripting web pages, you might not have explored this part of the language. Javascript is a dynamic language, allowing a free and easy approach where properties are created on the fly. For example, if you were writing code to manage a magazine archive, you might have something such as this:

mag = new Object();
mag.name = "PCW";
someLabel.innerHTML = "The magazine name is: " + mag2.name;

This code creates a new object, adds a property on the fly, and then references it. You can even add methods in similar style:

function describe()
{
return "The magazine name is: " + this.name;
}

mag = new Object();
mag.name = "PCW";
mag.getdescription = describe;
someLabel.innerHTML = mag2.getdescription();

This looks odd to someone who has coded in languages like Java, C# or Visual Basic .Net, because the function declaration is detached from the object definition, yet still uses the keyword ‘this’ to access the properties of the current instance.

There is more to say about object orientation in Javascript; but what is most interesting is that Actionscript 3.0 has a more conventional approach. You can write code like this:

public class Magazine
{
var name:String;

function getdescription()
{
return "The magazine name is: " + this.name;
}
}

var mag: Magazine
mag = new Magazine();
mag.name = "PCW";
someLabel.text = mag.getdescription();

Notice the strong typing, using a colon followed by a type name after the variable name. The way the class is defined looks much more like Java or C#. The full Ecmascript 4.0 specification allows for other features including interfaces, inheritance, property getters and setters, and all you would expect from a modern language.

Why not just use Java?
You could say these extensions are not needed. The language is losing some of its simplicity and becoming more like Java and C#. Why not just use these instead? It just happens that Adobe has come along with a lightweight but powerful cross-platform virtual machine.

Java and .Net programmers assume the presence of huge class libraries that are, in effect, part of the platforms, improving productivity but also complicating use. Javascript does not carry this baggage.

Perhaps the closest equivalent to Actionscript and Flex is Microsoft’s Windows Presentation Foundation Everywhere (WPF/E). This is meant to include a cross-platform Jit compiler for C#. WPF/E is interesting for .Net programmers, but Adobe has a substantial headstart with Flash and Flex, which is here now.

Resources
Adobe Flex downloads, including the free SDK, are at www.adobe.com/products/flex

Information on the Tamarin project is at www.mozilla.org/projects/tamarin

Testing Javascript performance
The performance of script code is often unimportant, but it matters when there is significant processing. An example might be a financial application that generated cost and income projections, or a game that has to calculate the positions of numerous sprites.

For this test, I used an algorithm called the Sieve of Eratosthenes, which identifies prime numbers. It is useful because the only known way to do this is to test each number, although there are obvious shortcuts such as eliminating even numbers.

Here are the results in seconds on the test machine for counting the number of primes up to 500,000:

Visual C++: 0.2
Internet Explorer: 11.8
Firefox: 11.0
Flash typed: 0.7
Flash untyped: 2.4
Jscript.Net untyped: 2.6
Jscript.Net typed: 0.6

The C++ code is three times faster than any of the others, and more than 50 times faster than the slowest. This is the main reason so many applications are still written in C or C++.

Moving on, in this example the Flash virtual machine is neck and neck with Microsoft’s .Net Jit compiler for Jscript. Since .Net is already widely used for business applications, the implication is that Adobe’s Flash runtime may perform equally well.

Third, there is a major performance benefit in using strong typing. To be fair, this kind of test exaggerates the typing factor because everything happens using a few variables in a tight loop. Real-world applications often have bottlenecks elsewhere.

Finally, the Jit compilation in the Flash 9 VM seems to have genuine performance benefits, even for untyped code. In this test, it is four times faster than the same code running in Firefox or Internet Explorer.

Jscript: Microsoft’s hidden .Net language
Microsoft supplies a .Net compiler for Jscript.Net, its version of Ecmascript, with every installation of the .Net Framework. However, this is well hidden, mainly because there is no obvious way to use it in Visual Studio 2005, although you can create and edit Jscript files.

Apparently the compiler can be used by Asp.net, but you can also compile .Net executables from Jscript at the command line. In Visual Studio, choose File, New, File, Script and then Jscript File. Alternatively, you can use any text editor and write some Jscript code, for example:

import System;
Console.WriteLine("Testing JScript");

Save the file as testjscript.js. Then open a Visual Studio command prompt, navigate to where you saved the script, and type:

jsc /out:testjscript.exe testjscript.js

Now you can run testjscript.exe like any other Windows executable.

Presumably most developers do not think this is a useful tool, otherwise Microsoft would have put more focus on it. On the other hand, if Javascript grows in popularity to the point where more programmers consider it their language of first choice, Jscript.Net might getmore use.

A crucial advantage of Jscript.Net over Actionscript is that you get access to all the class libraries in the .Net Framework. If you can manage without visual tools, there should be few limitations.

www.pcw.co.uk/2186585
This article was printed from the Personal Computer World web site
© Incisive Media Ltd. 2008
Incisive Media Limited, Haymarket House, 28-29 Haymarket, London SW1Y 4RX, is a company registered in the United Kingdom with company registration number 04038503
Close this window to return to the website