5467 posts The Dude Abides
  • Top Monthly Author
  • Trendsetter
  • Weekly Top Seller
  • United States
+13 more
CodingJack
says

Anyone know or have any suggestions? For example, in Actionscript, this:

if(num != (num | 0)) num = (num | 0) + 1;

is faster than this:

Math.ceil(num);

Is it also faster in JavaScript?

P.S. I know some optimizations carry very little gain but I still like to use them.

50 posts
  • Elite Author
  • Author Level 7
  • Most Wanted Winner
  • Winner
+8 more
cosmocoder
says

It is definitely faster to use bitwise operators to round numbers than using native javascript functions such as Math.round or Math.ceil or Math.floor.

Check out this article which explores these techniques and how fast they are – http://sebleedelisle.com/2011/02/html5-canvas-sprite-optimisation/

In order to know how fast your method is compared to Math.ceil use jsPerf. But in the end how much better performance you are gaining depends on your application and the way it is coded.

256 posts
  • 6 Years of Membership
  • Affiliate Level 4
  • Author Level 8
  • Belgium
+11 more
Sitebase
says

It’s important to look at how many times you perform a specific funtion. If you do that Math.ceil one time in your code, then I think it’s not worth the confusion these type of optimizations can give.

Make sure to check Google developer tools in Chrome. They have lot’s of great tools to help you profile and optimize your code.

5467 posts The Dude Abides
  • Top Monthly Author
  • Trendsetter
  • Weekly Top Seller
  • United States
+13 more
CodingJack
says
Sitebase said
It’s important to look at how many times you perform a specific funtion. If you do that Math.ceil one time in your code, then I think it’s not worth the confusion these type of optimizations can give.

Good point. I think for the type of stock I sell the majority of buyers aren’t editing the core code though (just the built-in options). So I usually optimize whenever possible, and it seems even more important with JavaScript because JS executes so slowly in some environments.

@cosmocoder, thanks for the links :)

4140 posts
  • 7 Years of Membership
  • Affiliate Level 1
  • Author Level 4
  • Brazil
+5 more
tsafi
says

You said it all little gain unless you’re app is based on lots of pure coding, but same as flash Math.floor is the slowest one .

In a more odds matter i can say with js it doesn’t matter, cos the js run on the browser platform and i don’t need to tell you the different on each browser when you run code on js since there is really no unity in Optimizing JavaScript on each type of browser.

What run fast on chrome can run slow on ff8 and each browser update brings its own illness with JavaScript Optimizing.

p.s.

Mmm I think it’s my first say here on cc ..probably will be my last one :P

6231 posts
  • Exclusive Author
  • Elite Author
  • Author Level 8
  • 7 Years of Membership
+13 more
VF
says
tsafi said
In a more odds matter i can say with js it doesn’t matter, cos the js run on the browser platform and i don’t need to tell you the different on each browser when you run code on js since there is really no unity in Optimizing JavaScript on each type of browser.

\\ I am getting dizzy, started dreaming about “one browser” world…

99 posts
  • Affiliate Level 1
  • Author Level 5
  • Collector Level 1
  • Top Monthly Author
+5 more
Lamovo
says
by
by
by
by
by
by