Javascript optional type hinting

36.1k views Asked by At

When a programming language is statically typed, the compiler can be more precise about memory allocation and thus be generally more performant (with all other things equal).

I believe ES4 introduced optional type hinting (from what I understand, Adobe had a huge part in contributing to its spec due to actionscript). Does javascript officially support type hinting as a result? Will ES6 support optional type hinting for native variables?

If Javascript does support type hinting, are there any benchmarks that show how it pays off in terms of performance? I have not seen an open source project use this yet.

5

There are 5 answers

9
Jeremy J Starcher On BEST ANSWER

My understanding, from listening to many Javascript talks on the various sites, is that type-hinting won't do as much to help as people think it will.

In short, most Javascript objects tend to have the same "shape", if you will. That is, they will have the same properties created in the same order. This "shape" can be thought of as the "type" of the object. An example:

function Point(x, y) {
  this.x = x;
  this.y = y;
}

All objects made from "Point" will have the same "shape" and the newer internal Javascript engines can do some fancy games to get faster lookup.

In Chrome (perhaps others), they use a high-bit flag to indicate if the rest of the number is an integer or a pointer.

With all of these fancy things going on, that just leaves typing for the human coders. I, for one, really like not having to worry about type and wouldn't use that feature.

You are semi-correct, though. Type hinting is a part of ActionScript 3 which is a derivative of ECMAScript -- but hinting has never made it into the standard. AFAIK, outside of wishful thinking, it hasn't been discussed.

This video describes things in far more detail: http://www.youtube.com/watch?v=FrufJFBSoQY

1
OOO On

ES7 (Not coming soon) has a new feature called guard might be the one you are asking. The syntax now is a little similar to ES4 and TypeScript. All use : and append the type to the variable. But its not confirm syntax.

1
Keldon Alleyne On

Javascript is prototype-based, so the 'type' of an object is entirely dynamic and able to change through its lifetime.

Have a look at Ben Firshman's findings on Javascript performance in regards to object types - http://jsconf.eu/2010/speaker/lessons_learnt_pushing_browser.html

1
Jason Orendorff On

JavaScript JIT compilers have to do some pretty fancy stuff to determine the types of expressions and variables, since types are crucial to many optimizations. But the JavaScript compiler writers have spent the last five years doing all that work. The compilers are really smart now. Optional static types therefore would not improve the speed of a typical program.

Surprisingly, type annotations in ActionScript sometimes make the compiled code slower by requiring a type check (or implicit conversion) when a value is passed from untyped code to typed code.

There are other reasons you might want static types in a programming language, but the ECMAScript standards committee has no interest in adding them to JS.

2
Nathan Wall On

I'm late, but since no one really answered you questions regarding the standards, I'll jump in.

Yes, type hinting was discussed as part of ECMAScript 4, and it looked like it was going to be the future of JavaScript... until ES4 bit the dust. ECMAScript 4 was abandoned and never finalized. ECMAScript 5 (the current standard) did not contain many of the things that were planned for ECMAScript 4 (including type hinting), and was really just a quickly beefed up version of the ECMAScript 3.1 draft -- to get some helpful features out the door in the wake of ES4's untimely demise.

As you mentioned, now they're working on churning out ECMAScript 6 (which has some totally awesome features!), but don't expect to see type hinting. The Adobe guys have, to a degree, parted ways with the ECMAScript committee, and the ES committee doesn't seem interested in bringing it back (I think for good reason).

If it is something you want, you might want to check out TypeScript. It's a brand new Microsoft project which is basically an attempt to be ES6+types. It's a superset of JavaScript (almost identical except for the inclusion of types), and it compiles to runnable JavaScript.