9

There are some algorithms which solve problems "very well" under the assumption that "very well" means minimizing the amount of floating point arithmetic operations in favor of integer arithmetic. Take for example Bresenham's line algorithm for figuring out which pixels to fill in order to draw a line on a canvas: the guy made practically the entire process doable with only some simple integer arithmetic.

This sort of thing is obviously good in many situations. But is it worth fretting about operations that require a lot of floating-point math in javascript? I understand that everything's pretty much a decimal number as far as the language specification goes. I'm wondering if it is practically worth it to try to keep things as integer-like as possible--do browsers make optimizations that could make it worth it?

Robz
  • 1,747
  • 5
  • 21
  • 35

3 Answers3

15

You can use Int8, Uint8, Int16, etc. in javascript, but it requires a bit more effort than normal - see TypedArrays.

var A = new Uint32Array(new ArrayBuffer(4*n));
var B = new Uint32Array(new ArrayBuffer(4*n));

//assign some example values to A
for(var i=0;i<n;i++)
  A[i] = i; //note RHS is implicitly converted to uint32

//assign some example values to B
for(var i=0;i<n;i++)
  B[i] = 4*i+3;  //again, note RHS is implicitly converted to uint32   

//this is true integer arithmetic
for(var i=0;i<n;i++)
  A[i] += B[i]; 

Recently, the asm.js project has made it is possible to compile C/C++ code to strange looking javascript that uses these TypedArrays in a rather extreme fashion, the benefit being that you can use your existing C/C++ code and it should run pretty fast in the browser (especially if the browser vendors implement special optimizations for this kind of code, which is supposed to happen soon).

On a side note* if you program can do SIMD parallelism (see wikipeda), i.e. if your code uses the SSEx instruction set, your arithmetic will be much faster, and in fact using int8s will be twice as fast as using int16s etc.

*I don't think this is relevant to browsers yet due to being too difficult for them to take advantage of on the fly. Edit: It turns out that Firefox is experimenting with this kind of optimization. Also Dart (true Dart, not Dart compiled to js) will be able to do this in Chrome.

Community
  • 1
  • 1
dan-man
  • 2,949
  • 2
  • 25
  • 44
  • 1
    "this is true integer arithmetic" → This is misleading! If you used multiplication instead, you would see cases where the two are different; they *are* going through a floating point intermediate. – Veedrac Aug 21 '17 at 01:32
4

Long ago, computers lacked dedicated FPUs and did floating point math entirely via software emulation.

Modern computers all have dedicated FPUs which handle floating point math just as well as integer. You should not need to worry about it unless you have a very specific circumstance.

StilesCrisis
  • 15,972
  • 4
  • 39
  • 62
  • "handle floating point math just as well as integer"--does it take the same amount of computational power to do a double-precision floating point divide as it would an integer divide on a modern CPU? – Robz Jan 03 '13 at 05:32
  • Let's not get pedantic. In general, for equal-size registers, FPUs do floating point math just as fast as ALUs do integer math. Division might be an exception. For addition, subtraction and multiplication, FPUs are rocket fast and can easily match an ALU. (Hell, floating point multiplication is simpler than integer multiplication.) – StilesCrisis Jan 03 '13 at 14:57
  • 2
    That's inaccurate. Even today, floating point math on a modern CPU is around 4-5x slower than integer math on the same size registers. – Lev Knoblock Mar 15 '21 at 20:12
-5

Actually, it makes no different. JavaScript has no concept of "integer". JS only uses double-precision floating-point numbers, which may or may not be integers.

Therefore, there is absolutely nothing to gain in terms of performance by limiting yourself to integers.

However, keep in mind that integers will be precise up to 251, whereas non-integers can very easily suffer from precision loss (example: 0.1), so you might gain because of this.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 12
    V8 (and probably other JavaScript engines) [tag values with types, including 31-bit signed integers](http://www.html5rocks.com/en/tutorials/speed/v8/). If you find you need to improve performance, using all integers may help. – Jim Jul 03 '14 at 04:07
  • 4
    Downvoted for innacuracy. Jim is right. Modern javascript absolutely has a concept of Integer, even if it doesn't have a word for it. – mako Feb 26 '15 at 22:30
  • I don't know if they had this in your day, but in mine, there is `Number.MAX_SAFE_INTEGER`. – Paul Draper Jul 21 '15 at 20:06
  • I've tested with the javascript v8 engine in chrome 70+, and I've found that floating-point arithmetic is slightly faster than integer arithmetic, though it may be implementation dependant, it's quite the basic test and could be inaccurate but here is the test. http://jsben.ch/HY8MG – Joshua Usi Sep 27 '19 at 10:35
  • @Experimentators There isn't much to optimize in the test you provide. Here is a better one that shows integers are generally faster: https://jsben.ch/e4pPH – Tomas Mar 12 '21 at 13:56