5322 posts The Dude Abides
    Located in United States Elite Author: Sold more than $75,000 on Envato Market Has sold $125,000+ on Envato Market Has been part of the Envato Community for over 5 years
+9 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.

46 posts
    Elite Author: Sold more than $75,000 on Envato Market Has sold $75,000+ on Envato Market and is now an Elite Author Made it to the Authors' Hall of Fame Sells items exclusively on Envato Market
+3 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
    Has been part of the Envato Community for over 5 years Has referred 100+ members Has sold $125,000+ on Envato Market Located in Belgium
+9 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.

5322 posts The Dude Abides
    Located in United States Elite Author: Sold more than $75,000 on Envato Market Has sold $125,000+ on Envato Market Has been part of the Envato Community for over 5 years
+9 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
    Has been part of the Envato Community for over 6 years Has referred 1+ members Has sold $5,000+ on Envato Market Located in Brazil
+3 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

5870 posts
    Sells items exclusively on Envato Market Elite Author: Sold more than $75,000 on Envato Market Has sold $125,000+ on Envato Market Has been part of the Envato Community for over 6 years
+8 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
    Has referred 1+ members Has sold $10,000+ on Envato Market Has collected 1+ items on Envato Market Made it to the Authors' Hall of Fame
+3 more
Lamovo says
by
by
by
by
by
by