3

In JavaScript, every number you will ever use will always be represented with what C programmer would call a double. The official type is I believe number. If I recall correctly, that fact was mentioned by Google as a "fundamental" problem with JavaScript and one of the reason why they wanted to have clean break with Dart. Now, I can't help but wonder :

  • Can the ECMAScript standard just add that type? Would it be possible to do without breaking existing code?
  • Anyway, can't a JIT engine watch the integer usage and generate code that is almost as efficient as using a native integer type, by using integer registers and opcodes?

In effect : Could it be done? Why should it be done?

Grant Miller
1432 gold badges2 silver badges11 bronze badges
asked Jul 15, 2013 at 3:03
3
  • 2
    Javascript 2.0 has that, and more. www-archive.mozilla.org/js/language/js20-1999年02月18日/types.html. Hopefully we can get support in modern browsers in the next few years. Commented Jul 15, 2013 at 4:26
  • 1
    Question: what does 9999999999999999 % 9 evaluate to? ;) Commented Jul 15, 2013 at 21:47
  • @Robert Harvey: maybe support in the next couple of decades, considering that spec is nearly old enough to drink. :-) Commented Apr 8, 2018 at 16:41

1 Answer 1

2

As for your second point, yes, integer use can be detected. Firefox does it as part of their asm.js feature. It's not entirely straightforward: for example, adding two integers may overflow. That wont happen if the values were added as double. To account for this, every expression that should produce a true 32 bit int is appended with | 0, which does nothing except forcing the result to be an int.

answered Jul 15, 2013 at 4:57
2
  • I see. Could it be done non asm.js code though? Commented Jul 16, 2013 at 15:05
  • Much like there are JS classes for complex numbers, you could write an int class that has add(), subtract() etc. and it does the | 0 trick. I suspect somebody has already done this??? Or are you more concerned about performance? Commented Apr 8, 2018 at 16:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.