5310 posts The Dude Abides
  • United States
  • Exclusive Author
  • Has been a member for 5-6 years
  • Elite Author
  • Bought between 100 and 499 items
  • Referred between 100 and 199 users
  • Author had a Free File of the Month
+5 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.

43 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Exclusive Author
  • Has been a member for 4-5 years
  • India
  • Referred between 50 and 99 users
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
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
  • Belgium
  • Beta Tester
  • Bought between 10 and 49 items
  • Elite Author
  • Exclusive Author
+4 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.

5310 posts The Dude Abides
  • United States
  • Exclusive Author
  • Has been a member for 5-6 years
  • Elite Author
  • Bought between 100 and 499 items
  • Referred between 100 and 199 users
  • Author had a Free File of the Month
+5 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
  • Bought between 1 and 9 items
  • Brazil
  • Exclusive Author
  • Has been a member for 6-7 years
  • Referred between 1 and 9 users
  • Sold between 5 000 and 10 000 dollars
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

5755 posts
  • Exclusive Author
  • Author had a File in an Envato Bundle
  • Elite Author
  • Has been a member for 5-6 years
  • Sold between 100 000 and 250 000 dollars
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • India
+3 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
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
  • Belgium
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Has been a member for 3-4 years
Lamovo says
by
by
by
by
by
by