tags:

views:

511

answers:

13

I'm confused, I regularly read talk of server-side JS, why would I want to use that? It seems like it would execute way slower than pretty much any other language, it also lacks many conventions that more sophisticated languages have.

Is it possible to hand entire objects from the client to the server, manipulate them and return them back?

Just struggling to understand the concepts of it.

A: 
mjboggess
dude, that describes pretty much nothing. I know Ruby, C, and PHP, I can build a web app with those... what are you talking about!
Joseph Silvashy
That article you've linked to is somewhere between obsolete and insane. The JavaScript v VBScript war ended a long time ago and VBScript was beaten into the ground.
Don
This stuff is 9 years old, man
mfeingold
haha, I think we are wasting down votes, I don't think we can vote him out of existence.
Joseph Silvashy
A: 

Server side JScript is just another language in .NET family of languages. In terms of syntax and features it is pretty much the Javascript we know from working client side with one obvious and very significant difference it is executed on the server.

Therefore the environment is the standard .NET environment you would expect for a program in any .NET language, and, in particular there is no DOM. The server side JScript is subject to the same limitations any other server-side program would be. In particular there is no way to directly pass a client-side JavaScript object to a server side program.

Performance wise JScript code is on par with any other language, because server side JScript is compiled, not interpreted. There is some overhead introduced by type coercions, but it is minimal.

As to why would you want to use JScript - primarily because it is a scripting language. If you need a language to be used by the users of your program to express some simple conditions and/or calculations you might find that giving them JScript as a way to provide this expressions is an attractive option. At least for me it worked out really well

mfeingold
Awesome insight. Thanks!
Joseph Silvashy
I don't know why someone tried to down-vote you... correct me if i'm wrong here, but JScript is a little different from JavaScript in that it contains some .NET specific implementations.
Joseph Silvashy
Also does that mean I couldn't really use server-side JS with an ubuntu server that is running rails?
Joseph Silvashy
@jpsilvashy - Not the .NET version (unless you are running MONO), and yes it has ways to deal with .NET stuff - classes and the such. The documentation is pretty sketchy though
mfeingold
You can pass objects between client and server and vice versa using JSON.JScript is IIS specific version of JavaScript. A more portable solution would be ECMA Script compliant NodeJS.
CyberED
+1  A: 

If you know the language and can operate with it at really high level, why would you want to switch to some other language? At this moment there is really nice and fast server-side JS, which is called nodejs. In my opinion nodejs proves that server-side JS is not fare-tale but reality.

nemisj
+6  A: 
  • I think you cant say anything about the speed of JavaScript without knowing where it is running. I think a V8 could beat a scripting languages like ruby or python.
  • In my opinion JavaScript is much more clearer language then for example PHP or Perl. But this is just my personal opinion.

So why not?

Interesting links: v8cgi, nodeJs, wxJavaScript and last but not least CouchDB (a JavaScript powered database server)

Hippo
Pretty cool info here, that explains a lot. I didn't realize that CouchDB was JS based! Don't get me wrong folks! I have nothing against JS, I just don't know what server side JS is for.
Joseph Silvashy
Don't forget that you can run JavaScript (JScript) server-side in Microsoft (classic) ASP pages too!
Funka
Also, the community for "standardizing" the server side javascript infrastructure (modules, packages, common APIs, ...) was set up less than a year ago and it now goes by the name CommonJS (formerly ServerJS). I recommend checking out the CommonJS home page (http://wiki.commonjs.org/wiki/CommonJS) and a reference implementation called Narwhal (http://narwhaljs.org/).
jedi
+8  A: 

Yes, you can hand entire objects from the client to the server, manipulate them and return them back, using JSON. You can also share code between the client and server.

Annie
+1 for the code sharing comment.
John MacIntyre
+6  A: 

why would I want to use that?

It reduces the number of languages your projects use. For a modern webapp, you need developers with some JavaScript knowledge anyway, and if you need only JavaScript expertise, you'll have a bigger labor pool to choose from than if you need JavaScript+Java or JavaScript+C#, or JavaScript+anything.

It seems like it would execute way slower than pretty much any other language,

Can you give any reason why you believe so? Surely this is entirely a matter of VM implementation and thus not something you can make definitive statements about. Besides, it's almost a truism that webapps are usually bottlenecked on DB access rather than CPU.

it also lacks many conventions that more sophisticated languages have.

Um... No? Nowadays, JavaScript (or, properly, ECMAScript) is a quite sophisticated language of its own. It has exceptions, closures, associative arrays... really, all you need.

Is it possible to hand entire objects from the client to the server, manipulate them and return them back?

Not the objects as such, since the underlying protocol is still HTTP, but using JSON is really almost the same thing.

Michael Borgwardt
+1 for shooting down all my assumptions!
Joseph Silvashy
+1  A: 

Don't forget you can get all your client-side goodness that Mootools et al give you but on the server side, make coding a sheer joy when you get that up and running. As for speed, depends what you what it to do. We have a site running 1000's of lines of code in JScript ASP per page request, 60K users per day, 1000's of pages. Its is lightening fast! So fast that the case for re-coding it in .net is just not strong enough as the current code handles it perfectly well.

Writting Javascript client side can hurt your head a bit at first, no elements to mess with just nice coding style with some really quirky feature (we pass an annoymous functions/closures into our DB code like so for instance:

var hotels = DataLayer.FindByHotelByStarRating( 4 )

hotels.each( function( hotel ) {
  %>
  <li><%= hotel.name.htmlEncode() %> is rated as <%= hotel.star_rating %> star</li>
  <%
}, this );

Now thats pretty powerful and damn fun too, and the time spent learnng Mootools or advanced JS is not wasted as we can use it both Server-side and client side.

We can use the same validation logic, the same JSON funcitons, the same Objects (if your careful) and so on. ASP with JScript is 100 times better/cleaner/simpler/nicer than VBScript and makes my job a joy rather than a VBScript head ache.

Plus most importantly, its damn fun!

Pete Duncanson
Hmm.. you say **all** of the client side goodness, but really most of the client side stuff these libraries provide is mostly DOM manipulation, yah? Awesome code example! +1
Joseph Silvashy
Hmm your right I was more referring to the Class, String, Function, Number, Date, Events parts of Mootools that we use. JS is not just about DOM stuff however. We obviously can't use HTML DOM server-side but we have been inspired to create a nice DOM wrapper for our MSXML Doc object which give us some of the nice short cuts for navigating around XML DOM's server side, really should release that one of these days :)
Pete Duncanson
Mootools have a specific Server-Side version fo their goodies: http://mootools.net/download saves you having to hack your own version from the client-side download like we had to :)
Pete Duncanson
+1  A: 

I'm confused, I regularly read talk of server-side JS, why would I want to use that?

So that you could write all your apps in a single language. So that you could share objects between server and client without going through serialization/conversion/etc. So that you could write code onetime (eg. field validation)

It seems like it would execute way slower than pretty much any other language,

Not true. The perception of JS being slow is due to being used client side. Engine implementations were quite slow and there was also the DOM issues. It has been shown[1] that JS can come close to C in performance. And this was last year. There have been further performance improvements. Also check out the [benchmarks of Node.js webserver agains others[2].

it also lacks many conventions that more sophisticated languages have.

You mean stuff like [closures, generators, map/reduce[3], [higher order functios, dynamic typing and a prototype based OO paradigm, more flexible than class-based OO[4]?

Is it possible to hand entire objects from the client to the server, manipulate them and return them back?

It[5] is[6].

-- MV

1: shaver.off.net/diary/2008/08/22/the-birth-of-a-faster-monkey/ 2: four.livejournal.com/1019177.html 3: ejohn.org/blog/javascript-18-progress/ 4: www.ibm.com/developerworks/java/library/j-cb12196/index.html 5: www.jaxer.org/ 6: juicejs.org/

mvalente
+1  A: 

ah however aren't those interpreters part of the clients browsers, they probably won't have those available to their server

Check out www.commonjs.org

-- MV

mvalente
A: 

Hmm.. you say all of the client side goodness, but really most of the client side stuff these libraries provide is mostly DOM manipulation, yah?

They do. But you could use jquery (for example) to generate HTML serverside.

-- MV

mvalente
+1  A: 

Javascript isn't slow; The DOM is.

+1  A: 

Having used nodejs for a while now and having written a mongodb driver for it I have to say that I'm very impressed. First of all since I'm using mongo which stores JSON objects I have a client to db pipeline, that's all javascript.

So while I'm still using ruby at work I have moved to Javascript for all personal projects because I'm lazy and can't stand the constant context switching between SQL, Ruby, Javascript.

And if for nothing else. NodeJS is a fantastic way to re-learn a language few of us ever invested much energy in, and that can only make you a stronger web developer :D

christkv
+1  A: 

Hi Joe,

I came in late. JavaScript / ECMA-Script is a basic language that can be bound to any library. When used in a browser, it exposes several browser level objects (window, document, etc) to the language. The scripts that you embed in .html files or link as .js files are programmed to perform desired operations upon those objects.

The same concept applies on the server-side. For example, NodeJS exposes HTTP Server and Client objects and you can do the same sort of things that you would in PHP, CGI scripts, Python or Ruby frameworks or Apache modules.

Google's V8 JavaScript engine with JIT is very well optimised. It is used in both Chrome (browser) and NodeJS (server).

I used to use JavaScript in the browser making AJAX calls and Python / WSGI for the server. This mixed mode of programming annoyed me. Since I've discovered NodeJS I can do both Server-side and Client-side programming in the same language. Surprisingly, NodeJS throughput is more than double that which I achieved using Python. There are some more detailed analyses at nodejs.org. The simple take-away is that JS when using V8 comes very close to C/C++ speeds.

CyberED