13

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.

badunk
  • 4,310
  • 5
  • 27
  • 47
  • "I believe ES4 introduced optional type hinting" => do you have a citation for that? – Richard JP Le Guen Sep 11 '12 at 23:20
  • I am not sure if this counts, but here is the spec: http://www.ecmascript.org/es4/spec/overview.pdf You will find the info under Types by searching the pdf for "optional" – badunk Sep 11 '12 at 23:30
  • 3
    Oh... ES4 - AKA "The Standard that Wasn't." Without getting into the politics of it, consider ES4 "Wishful thinking." – Jeremy J Starcher Sep 11 '12 at 23:33

5 Answers5

12

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

Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
  • Is the fancy stuff you're talking about TraceMonkey, V8 and it's ilk? – Jared Farrish Sep 11 '12 at 23:42
  • Interesting: [*Incremental Dynamic Code Generation with Trace Trees*](http://www.ics.uci.edu/~franz/Site/pubs-pdf/ICS-TR-06-16.pdf) – Jared Farrish Sep 11 '12 at 23:44
  • @JaredFarrish -- I watched like five hours of JS related videos in a row, some of the details got blurry. I know the specific examples that I gave came from `V8`, but there were comments along the line of "And the Firefox team does something similar." – Jeremy J Starcher Sep 11 '12 at 23:52
  • Linked in a documentary video to my answer. – Jeremy J Starcher Sep 11 '12 at 23:56
  • Well, it seems that [TraceMonkey is no more](http://blog.mozilla.org/nnethercote/2011/11/23/memshrink-progress-report-week-23/), and [JägerMonkey](https://wiki.mozilla.org/JaegerMonkey) is now supreme. Interesting article. Also, whatever happened to running C++ or whatever in Chrome? Is that still possible? – Jared Farrish Sep 11 '12 at 23:59
  • Well, answered my own question. It's called [Native Client (`NaCL`)](https://developers.google.com/native-client/) and it's still seemingly active. – Jared Farrish Sep 12 '12 at 00:01
  • This answer wasn't the most direct, but the takeaway was that this ES4 feature is not/will not be supported. – badunk Sep 12 '12 at 01:04
  • Correct. ES4 is dead. ES5 took some ideas and built from there. – Jeremy J Starcher Sep 12 '12 at 01:30
  • This answer didn't age well – birgersp Aug 25 '22 at 07:15
8

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.

Nathan Wall
  • 10,530
  • 4
  • 24
  • 47
5

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.

Jason Orendorff
  • 42,793
  • 6
  • 62
  • 96
  • There are projects like asm.js that manage to get near-native performance with JavaScript (or so they say anyway) by doing weird tricks with type coercion. Type hints can greatly help with this, at least in theory. –  Nov 16 '16 at 19:19
3

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.

OOO
  • 324
  • 2
  • 5
  • 1
    ecmascript.org is down. https://web.archive.org/web/20140728005246/http://wiki.ecmascript.org/doku.php?id=strawman%3aguards –  Jun 08 '17 at 19:08
0

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

Keldon Alleyne
  • 2,103
  • 16
  • 23