Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript optional type hinting

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.

like image 201
badunk Avatar asked Sep 11 '12 23:09

badunk


People also ask

Does JavaScript have type hinting?

Typed JavaScript is a system of tooling and conventions that exposes type hints and type errors before a program is run.

Which data types make TypeScript superior to JavaScript?

Advantages of using TypeScript over JavaScript TypeScript supports static/strong typing. This means that type correctness can be checked at compile time. This feature is not available in JavaScript. TypeScript is nothing but JavaScript and some additional features i.e. ES6 features.

Whats the difference between TypeScript and JavaScript?

In a nutshell, TypeScript is an object-oriented programming language, whereas JavaScript is a scripting language. Thus, TypeScript offers interfaces and modules through ES6 features; on the other hand, JavaScript doesn't offer such features.

What is typed JavaScript?

JavaScript typed arrays are array-like objects that provide a mechanism for reading and writing raw binary data in memory buffers. Array objects grow and shrink dynamically and can have any JavaScript value. JavaScript engines perform optimizations so that these arrays are fast.


1 Answers

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

like image 125
Jeremy J Starcher Avatar answered Sep 19 '22 17:09

Jeremy J Starcher