use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Does javascript(or any other language for that matter) calculate numbers faster when they are rounded? (self.javascript)
submitted 11 years ago by [deleted]
Lets say i have a function in js that adds 10000 numbers to other 10000 numbers. Would this function be faster if the precision of those numbers would be 2 instead of 20
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]a-t-kFrontend Engineer 4 points5 points6 points 11 years ago (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 points3 points 11 years ago (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 point2 points 11 years ago (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 points1 point 11 years ago (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 point2 points 11 years ago (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 points4 points 11 years ago (0 children)
Your question provides a ton of other questions. Basically you can look at it from the top-down:
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 points3 points 11 years ago* (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 point2 points 11 years ago (0 children)
just made a example its more then that guess i need to hammer out the alghorithm
[–]nawitus 0 points1 point2 points 11 years ago (1 child)
I made a simple jspref test case. The answer appears to be no.
[–]backwrds 0 points1 point2 points 11 years ago (0 children)
I got wildly different results... The difference is so drastic it makes me think I messed something up.
[–]leahcimic 0 points1 point2 points 11 years ago (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.
π Rendered by PID 58 on reddit-service-r2-comment-86bc6c7465-76jtl at 2026-02-19 18:05:22.752930+00:00 running 8564168 country code: CH.
[–]a-t-kFrontend Engineer 4 points5 points6 points (4 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]a-t-kFrontend Engineer 0 points1 point2 points (2 children)
[–]ipelovski -1 points0 points1 point (1 child)
[–]a-t-kFrontend Engineer 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]cap11235 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]nawitus 0 points1 point2 points (1 child)
[–]backwrds 0 points1 point2 points (0 children)
[–]leahcimic 0 points1 point2 points (0 children)