Datasheet
Chapter 1: Introduction to JavaScript
5
3.5
3.209
Performance Comparison - Safari
Desktop vs. iPhone
100,000
iterations
10,000
divisions
10,000
sin(x) calls
10,000
string
allocations
10,000
function
calls
0.413
0.709
0.777
0.904
3
2.5
2
1.5
Seconds
1
0.5
0
0.041
0.005 0.009 0.01 0.01
iMac iPhone
Figure 1-1
One of the key considerations when writing JavaScript for mobile platforms is the abysmal performance
offered by these devices, as illustrated by the graph in Figure 1 - 1. In these cases, it becomes even more
important to use best practices for high - performance code. Many of these are described in Chapter 25.
When used in a browser, JavaScript is considered an interpreted language . This sets it apart from other
programming languages such as C++, a compiled language . When a browser downloads a page with
JavaScript embedded, it receives the original source code of the script. It then passes the script to a
program called an interpreter, which converts it to machine code on the fly. The browser does this every
time it loads the page and does not attempt to cache or validate the program before it is executed. Errors
are passed on to the user as they occur. The advantage for the developer is that it is a very lightweight
way to write applications and the main debugging environment is the browser itself. The disadvantage
is that all your source code is visible to anyone that wants to see it. Also, because it is interpreted on the
fly, not compiled to machine code first, JavaScript is not suitable for writing CPU - intensive applications
like a 3D game or Autocad program, mainly because it won ’ t be fast enough.
Server - Side JavaScript
Although the development context is different, JavaScript has also been implemented many times over
as a server - side scripting language , often to generate web pages. This was first done by Netscape as part of
their Enterprise Server 3.0 product in the form of a feature called LiveWire . That was in 1996. Today there
are many server - side frameworks implementing JavaScript. Some of these use open source interpreters
such as Rhino or SpiderMonkey . Microsoft uses their interpreter (called JScript ) in both their browser and
their development runtime .NET . Even the now - obsolete ASP framework from Microsoft had JScript as
CH001.indd 5CH001.indd 5 6/25/09 7:53:16 PM6/25/09 7:53:16 PM