- United States
- Has been a member for 4-5 years
- Exclusive Author
- Author was Featured
- Sold between 50 000 and 100 000 dollars
- Item was Featured
- Contributed a Tutorial to a Tuts+ Site
- Author had a Free File of the Month
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.
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.
- Author had a File in an Envato Bundle
- Author had a Free File of the Month
- Author was Featured
- Belgium
- Beta Tester
- Bought between 10 and 49 items
- Exclusive Author
- Has been a member for 3-4 years
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.
- United States
- Has been a member for 4-5 years
- Exclusive Author
- Author was Featured
- Sold between 50 000 and 100 000 dollars
- Item was Featured
- Contributed a Tutorial to a Tuts+ Site
- Author had a Free File of the Month
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 
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 
- Exclusive Author
- Item was Featured
- Author was Featured
- Author had a File in an Envato Bundle
- Has been a member for 4-5 years
- Sold between 100 000 and 250 000 dollars
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- India
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…
A good overview: http://youtu.be/mHtdZgou0qU
