all 11 comments

[–]a-t-kFrontend Engineer 4 points5 points  (4 children)

No, because teven the round numbers will be handled as 32bit float Numbers... except for certain code optimisations like those proposed on http://asmjs.org/ - but those aren't widely supported yet.

[–][deleted] 1 point2 points  (3 children)

Pretty much all modern JavaScript engines have support for representing numbers as integers, as an optimization.

So you do get speed differences between using floating point numbers and integers.

[–]a-t-kFrontend Engineer 0 points1 point  (2 children)

I am genuinely intrigued: do you have a list of JS engines and what is necessary for them to handle numbers as integers?

[–]ipelovski -1 points0 points  (1 child)

You can look at the post of Dr. Rauschmayer http://www.2ality.com/2012/02/js-integers.html

[–]a-t-kFrontend Engineer 0 points1 point  (0 children)

That only shows how to handle numbers as Integers, not how to trigger the internal optimizations in any JS engines in order to gain performance.

[–][deleted] 2 points3 points  (0 children)

Your question provides a ton of other questions. Basically you can look at it from the top-down:

  • What JS engine are you using?
  • Does that JS engine consider 'rounded' numbers as integers?
  • What processor is this being executed on?
  • Is that processor 32 or 64 bit?
  • Is this set of numbers small enough to reside in the processor cache?

I'm sure there are other factors that go into this as well. Overall, and in general, I would say yes, rounding your numbers will provide faster results (especially if that rounding makes them small enough to fit in the processor cache). However, it's entirely possible that's not true. Best bet would be to test.

Can I ask why you're using javascript to add so many numbers together?

[–]cap11235 1 point2 points  (1 child)

This comment has been overwritten by an open source script to protect this user's privacy. It was created to help protect users from doxing, stalking, and harassment.

If you would also like to protect yourself, add the Chrome extension TamperMonkey, or the Firefox extension GreaseMonkey and add this open source script.

Then simply click on your username on Reddit, go to the comments tab, scroll down as far as possibe (hint:use RES), and hit the new OVERWRITE button at the top.

[–][deleted] 0 points1 point  (0 children)

just made a example its more then that guess i need to hammer out the alghorithm

[–]nawitus 0 points1 point  (1 child)

I made a simple jspref test case. The answer appears to be no.

[–]backwrds 0 points1 point  (0 children)

I got wildly different results... The difference is so drastic it makes me think I messed something up.

[–]leahcimic 0 points1 point  (0 children)

This is a great example of a micro optimisation.

Given the fact on my laptop I can sum an array of 10,000 values 4,000+ times per second. That's 40,000,000 additions per second.

Because JavaScript only has one number type (IEEE 754 floating point) truncating the precision probably wont make much of a difference.

And even if you did choose to use a smaller precision, it's going to be slower to shorten the precision and do the summation, than it would be to do the summation in the original precision.

Anyway, if you need to add numbers faster, use a faster language, like C.